Jump to content
Sign in to follow this  
Horner

Apparently exact values are way too mainstream.

Recommended Posts

Just trying to grab the damn decimals in a number, but no..

_num = (100 + 0.123);
_num = _num - (floor _num);
player groupChat (str _num);

This prints out "0.123001"

When I print out "(100 + 0.123)" it prints out correctly, is it floor that isn't working?

Is there any way to get a true exact value out of this? I'd just like 100.123 - 100 to evaluate properly.

Share this post


Link to post
Share on other sites

Try

_num mod 1

Arma stores numbers single precision floating point numbers, which can cause unexpected and imprecise results, especially when using extremely large or extremely small numbers.

Share this post


Link to post
Share on other sites

the way I've always done it, not for the same reason though.

_num = (100 + .123);
_num = (floor(((_num - (floor _num))*1000) mod 1000))/1000;
player groupChat (str _num);

Share this post


Link to post
Share on other sites

Sad thing is it doesn't fluctuate for all numbers, Squeeze, and _num mod 1 has the same effect as floor, ceeeb.

Share this post


Link to post
Share on other sites

Horner, have you tried CBA_fnc_formatNumber?

Used in a trigger, this seems to get what you want:

null = [] spawn {_num = (100 + 0.123);  _num = _num - (floor _num);  player groupChat (str _num); test = [_num, 1, 3] call CBA_fnc_formatNumber; player groupChat test;};

Share this post


Link to post
Share on other sites

Please look into floating point numbers Horner (I went through the same confusion myself some years ago). They can not store all decimal numbers precisely, and round off to the nearest possible value.

http://en.wikipedia.org/wiki/Floating_point - Wikipedia article on floating point numbers

http://www.h-schmidt.net/FloatConverter/IEEE754.html - online Java tool to convert decimal to floating point numbers. Note that 100.123 is not a value that can be stored as a floating point number (closest possible number is 100.123001)

Share this post


Link to post
Share on other sites

@panther, I'm actually working on a different number formatting system than CBA because of how the "mod" operator starts to spit out odd values after 100 billion (I've managed to fix that). But I could look into formatNumber for adding decimals. Thanks.

@ceeeb, Thanks for the documentation on that, I'll read up, I'm aware now that arma uses float values but it seems to only mess up when using operators other than the standard + - * /.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×