Printing high digits error.

Started by Rocky, Aug 23, 2016, 08:09 AM

Previous topic - Next topic

Rocky

Its been a while since i coded. :/
local i=0.000000000000000000000000000001;
Calc = format("%.30f", i );
print( Calc );

OUTPUT: 0.000000000000000000000000000001

However this doesn't work:

local i=0.001000000000000000000000000001;
Calc = format("%.30f", i );
print( Calc );

OUTPUT: 0.0010000000474974513000000000

Don't ask me why i need to use these high numbers. I just need to :X



.

Welcome to floating point precision issues. That's very normal behavior.
.

EK.IceFlake

#2
https://en.wikipedia.org/wiki/Arithmetic_overflow
To solve this you need to store them in a string. Also you cant do math functions directly, i.e. you cant do
[spoiler=Beware, assembly ahead]add eax 0.0010000000000000000001[/spoiler]
you need to use your own add function which stores data in strings or to the hard drive instead of the memory

.

.

EK.IceFlake

Quote from: . on Aug 23, 2016, 12:57 PM
Quote from: EK.CrystalBlue on Aug 23, 2016, 12:33 PMhttps://en.wikipedia.org/wiki/Arithmetic_overflow

Wrong link. This is not an overflow. That's just how floating point numbers work.
It must have a name. I dont think it has a name.
Its most closely related with an overflow IMO

Rocky

Quote from: . on Aug 23, 2016, 12:57 PM
Quote from: EK.CrystalBlue on Aug 23, 2016, 12:33 PM...

Wrong link. This is not an overflow. That's just how floating point numbers work.

Any way i can counter this in squirrel or i don't really have to use squirrel to do what i am doing right now since its a bit unrelated to vc-mp (some experiments).  So, Just wondering if this issue is present in all languages such as C, Java etc as well.



Quote from: EK.CrystalBlue on Aug 23, 2016, 12:33 PMhttps://en.wikipedia.org/wiki/Arithmetic_overflow

If i'm right Arithmetic overflow generally refers to when trying to store some value that is too large to be represented within the available storage space of a certain variable. (I've lost touch so i'm not completely sure about these terms.)

.

Quote from: Rocky on Aug 23, 2016, 02:49 PMAny way i can counter this in squirrel or i don't really have to use squirrel to do what i am doing right now since its a bit unrelated to vc-mp (some experiments).  So, Just wondering if this issue is present in all languages such as C, Java etc as well.

Squirrel is created in C/C++. Therefore, this issue with floats comes from C/C++. There's nothing you can do about it.

Your only option here is to move to x64 architecture where integers have a range of -9223372036854775808 to 9223372036854775807. Or, if you need long floating point numbers. Compile the plugin your self and force Squirrel to use double floating point precision (doubles).



I remember I had this issue as well when I was working on my plugin. And as a workaround for the x32 bit systems I simply made 2 helper classes that can encapsulate x64 bit integers.

Ex:
local s = SLongInt("2165125312309463", 0);
print(s);
s += SLongInt(2);
print(s);

Output:
[USR] 2165125312309463
[USR] 2165125312309465

However, I need to revise it to make it seem like it's a part of the language. This was just a test.
.

Stormeus