# Thread: Value of Real variable getting corrupted..

1. ## Value of Real variable getting corrupted..

I have a large number (more than 13 digits) in string format. Since I cannot use the Val() function I tried to write a simple function of my own to convert it into a real number. The function works fine if the number is upto 16 digits, but the Real variable that i use gets corrupted when the number length exceeds 16. I was just curious as to why this was happening. Below is the code along with the results. Has any one come across this problem before.

Here's the code
<font class="small">Code:</font><hr /><pre>
[-] REAL RealVal(STRING sNumber)
[ ] REAL rValue = 0
[ ] INTEGER iTemp
[-] for iTemp = 1 to Len(sNumber)
[ ] rValue = (rValue*10) + Val(sNumber[iTemp])
[ ] Print("iTemp={iTemp}, Digit used = {sNumber[iTemp]}, rValue={rValue}")
[ ] return rValue

</pre><hr />

Here's the result
<font class="small">Code:</font><hr /><pre>
[ ] iTemp=1, Digit used = 1, rValue=1.000000
[ ] iTemp=2, Digit used = 0, rValue=10.000000
[ ] iTemp=3, Digit used = 0, rValue=100.000000
[ ] iTemp=4, Digit used = 1, rValue=1001.000000
[ ] iTemp=5, Digit used = 1, rValue=10011.000000
[ ] iTemp=6, Digit used = 2, rValue=100112.000000
[ ] iTemp=7, Digit used = 2, rValue=1001122.000000
[ ] iTemp=8, Digit used = 3, rValue=10011223.000000
[ ] iTemp=9, Digit used = 3, rValue=100112233.000000
[ ] iTemp=10, Digit used = 4, rValue=1001122334.000000
[ ] iTemp=11, Digit used = 4, rValue=10011223344.000000
[ ] iTemp=12, Digit used = 5, rValue=100112233445.000000
[ ] iTemp=13, Digit used = 5, rValue=1001122334455.000000
[ ] iTemp=14, Digit used = 6, rValue=10011223344556.000000
[ ] iTemp=15, Digit used = 6, rValue=100112233445566.000000
[ ] iTemp=16, Digit used = 7, rValue=1001122334455667.000000
[ ] iTemp=17, Digit used = 7, rValue=10011223344556676.000000
[ ] iTemp=18, Digit used = 8, rValue=100112233445566750.000000
[ ] iTemp=19, Digit used = 8, rValue=1001122334455667500.000000
[ ] iTemp=20, Digit used = 9, rValue=10011223344556675000.000000
[ ] iTemp=21, Digit used = 9, rValue=100112233445566740000.000000
[ ] String used= 100112233445566778899 :: Value from function = 100112233445566740000.000000

</pre><hr />

2. ## Re: Value of Real variable getting corrupted..

Hi,

I think what you are experiencing is a reduced precision because of the large numbers you are trying to represent with the floating point number. For more information about that, check out how floating point numbers work: http://en.wikipedia.org/wiki/Floating_point_numbers

If you need big numbers like that with absolute precsion, maybe it helps to use two integers in a record, one being the lower part and one being the high part of the number.

3. ## Re: Value of Real variable getting corrupted..

Thanks tehlexx.. [img]/images/graemlins/smile.gif[/img]

4. ## Re: Value of Real variable getting corrupted..

It would help if we knew what you were trying to do with large numbers.

If you're trying to validate manual entry of say an order number or delivery reference, you can (for example) iterate through the string value and test each digit with IsDigit().

As Tehlexx says, you're out of luck if you want to convert the string number to a REAL or INTEGER.

Regards
peat

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.