Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC] Replacements for Val() and Str$() Functions

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 28th Mar 2008 13:34 Edited at: 15th Apr 2008 15:18
Just thought I should try and make things a little clearer, and correct another minor flaw I'd found in the Format Float routine (I promise, I'll check my code a lot better in future!). I'd found that the DBC Val() function broke down at certain numbers, so I created these based on some pseudocode Latch gave me.

String to Int function - for DBC integers and the DBPro "Integer" data type.
This includes a validation check, returning 0 if any non-digit characters are found, rounding off decimal places (5 is rounded up automatically) and returning +/-999999999 if the value of the input string exceeds this (since any larger numbers could exceed the assigned four bytes).



String to Float function - for DBC floats and the DBPro "Real" data type.
This includes a validation check, returning 0 if any non-digit characters are found or if more than one decimal place has been entered, and returning +/-999999999 if the value of the input string exceeds this (since any larger numbers could exceed the assigned four bytes).



String to DoubleInt function - for DBPro "Double Integer" data type ONLY!
This function has not been tested, since I do not own DBPro and so cannot compile it. I was trying to get it working for Calcyman (see later posts), but I believe this will now work. The problem was that I as trying to construct an 8-byte integer using a 4-byte variable - result, misery. The New line of code at the start of the function "NewInteger as DOUBLE INTEGER" should take care of this. Please post if you have any problems.

The function includes a validation check, returning 0 if any non-digit characters are found, rounding off decimal places (5 is rounded up automatically) and returning +/-999999999999999999 if the value of the input string exceeds this (since any larger numbers could exceed the assigned eight bytes).



String to DoubleFloat function - for DBPro "Double Float" data type ONLY!
as with the above function, this has not been tested. However, the new line of code at the start of the function "NewFloat as DOUBLE FLOAT" should take care of this. Please post if you have any problems.

The function includes a validation check, returning 0 if any non-digit characters are found, rounding off decimal places (5 is rounded up automatically) and returning +/-999999999999999999 if the value of the input string exceeds this (since any larger numbers could exceed the assigned eight bytes).



I was envious of DBPro's Str$() command, with the option to format the returned string to a set number of decimal places. I therefore created this (now updated to remove a couple of minor rounding bugs when the decimals hit something like 0.09999). This is for DBC only, as DBPRo has already got it:

If an exponential is found, the algorithm can follow one of four modes
1 = return exponential string
2 = remove exponential, return a pure decimal string
3 = remove exponential, return a decimal formatted to dp
4 = return exponential, decimal string formatted to keep a 12 digit string (13 digits if a minus sign is present)
If no exponential is found, the number is rounded like an ordinary float, and riounded to the specified number of decimal places.



Hope these are of use, and sorry for all the bother.

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 29th Mar 2008 19:07
Sorry to double post, but I just found a flaw in my Str_To_Float Function and I wanted to make sure the people who'd viewed this realised. When handling negative floats, the function would treat the minus sign as a digit in the string (since it converts the digits to ASCII codes, converts them to a number and then increments the float by that number raised to a certain power of ten). My first post has now been edited to show the corrected version - I'm terribly sorry about all this!

Did you hear about the escapologist's funeral? He was buried on June 25th, 26th, 27th, 28th...
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 5th Apr 2008 21:50
A 4-byte integer can store upto 2,147,483,648, and a float (although inaccurate) can store upto about 1e37. Your function will be useful if it can turn strings into QWORDS (double integers and double floats)

The optomist's right, The pessimist's right.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 5th Apr 2008 23:22
Hmmm...
It wouldn't be too hard to add more digits in, so I'll have a look over the next day or two and see what I can come up with. Of course, as I don't have DBPro I'm afraid it'll be up to you to tell me if it works...

Did you hear about the escapologist's funeral? He was buried on June 25th, 26th, 27th, 28th...
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 7th Apr 2008 13:15
Ok, calcyman , I might be mistaken on this but I think this will work for DBPro's Double Integer range. It was surprisingly easy to update, as the only part that needed changing was the "max safe value". With 32-bit integers (Dwords?), anything above 2147483647 goes haywireso the max safe value is the next lowest power of 10 - i.e. 999999999. For DoubleInt, the max allowed number is 9223372036854775807, so the max safe value is 999999999999999999

Here's the function for String to DoubleInt:




And here's the function for String to Double Float, but I'm not really sure how this will handle. I know that a DoubleFloat allows a max of 15 digits, but this one allows a float whose integer part has up to 18 digits. If the integer has more than 18 digits, then 999,999,999,999,999,999 will be returned - this may not be a good thing, as it doesn't take into account any decimals that are there or rounding that is required.



Hope these are what you need. If they don't work, or you get a number full of nines when you shouldn't do, let me know and I'll have another look at the code. I've already found (and now corrected) a flaw in Str_To_Float because it only ever checked the length of the initial string, and not the integer part - as a result, entering 100.123456 made the function returned 999999999! Oops...

Did you hear about the escapologist's funeral? He was buried on June 25th, 26th, 27th, 28th...
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 8th Apr 2008 11:30
Your str_to_doubleInt is safe upto about 2 billion, at which point it returns negative numbers. It handles rounding very well, though.

The optomist's right, The pessimist's right.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 8th Apr 2008 15:22
I think I know what the problem is - it's that the robot behind the computer screen doesn't understand computer terminology!!!

In DBC, an integer variable is simply called integer. But it's a 4-byte integer, sometimes called a Dword and also called a DoubleInteger! My original Str_To_Int will handle DoubleInt, because that's wwhat DBC works with.

The function I called Str_To_DoubleInt should probably be QuadrupleInt (if there's such a thing - I mean an 8 byte integer).

I've possibly made the same mistake with DoubleFloat (str_to_Float is for a 4-byte float, Str_To_DoubleFloat is for an 8 byte float), but it might be useful with certain data types in DBPro.

Quote: "Your str_to_doubleInt ... handles rounding very well, though."

At least it does something right

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 8th Apr 2008 17:25
Double integers are actually 8-byte.

The optomist's right, The pessimist's right.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 9th Apr 2008 11:01
Oh...

In that case, the only thing I can think of is that my variable "NewInteger" in the Str_To_Int function is actually being created as an Integer data type and not as a DoubleInteger. In DBPro, what's the default "Setting" when an integer variable is created? If it's a 4-byte integer, then only 4-bytes maximum will be for returned "NewInteger" without running into trouble. How would I create this variable as an 8-byte integer?

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Apr 2008 17:30
@RTR

If you are trying to store a 64 bit integer in DBC, you could use 2 32 bit DWORDS in a memblock. You'd have to handle the byte values for each half of the number. If DBPro supports a QWORD (4 * 2 bytes) then there probably is a command like

value = MEMBLOCK QWORD(memblock,position)

You'd still track the sign from the string and use the flag to output the negative:

if negflag then value=0-value

Enjoy your day.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 10th Apr 2008 00:22
It's an interesting idea Latch, but I wasn't actually trying to store an 8-byte integer in DBC, Calcyman was asking me to make a function that would:
Quote: "turn strings into QWORDS (double integers and double floats)"


I'm assuming that this was for DBPro only (judging by the references to DoubleInt) and the function should work if only I use a DoubleInt variable (8-bytes) to construct the output integer. I'm starting to get out of my depth here, and as I said, I'm hampered because I don't have DBPro.

Maybe I should create a Type statement, with one component of DoubleInt and then create an array of this Type. If yes, though, erm... How?

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!
calcyman
16
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 10th Apr 2008 13:55
There's an expansion pack which comes with DarkMATTER which gives DBC lots of DBPro commands.

The optomist's right, The pessimist's right.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 10th Apr 2008 22:21
Yes, I've got it (I've got V1.20) but it didn't allow for DoubleInt variables. I think I'm right about what the problm is - if the function doesn't use an 8-byte integer to generate the return number, it's not going to be able to poduce a double int.

I've been having a look round, how about placing this at the top of the doubleInt function:


and then every reference to NewInteger becomes:


I may have the syntax a little wrong, but this is the kind of thing I have in mind. Any good?

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 15th Apr 2008 15:20
Just edited the first post - I think I've got some working functions for QWords (8-byte integers and floats) but like I say, I haven't been able to test them. If some kind person with DBPro could just compile them and let me know how they work out, I'd be very grateful. Thanks!

It is said there are 10 types of people in this world - those who understand binary, and those who have friends!

Login to post a reply

Server time is: 2024-05-04 23:24:29
Your offset time is: 2024-05-04 23:24:29