ID:1851988
 
(See the best response by Lummox JR.)
Code:
mob
var
Dollars=0
verb
Add_Cash()
Dollars+=0.01
world<<"You now have $[Dollars]."


Problem description:
I am trying to get a dollar amount to show up in my game and it keeps failing by showing a lot more decimal places than there are numbers even, I used round() to round the number off properly but is BYOND not supposed to work with decimals because even the simple code above can't count to 1 without messing up.

It's telling me (You now have $0.839999.) And that's not even possible, so is BYOND meant to work with decimal values in the numbers or not?

I can also divide by 100 and use whole numbers as a solution, but then since BYOND's precision goes to 2^24 or 16,777,216 when adding 1 then my precision would affectively be 167,772.16
Best response
Floating point numbers are binary-based, not decimal. So while you can have 16,777,216 for an integer, dividing by 100 would not be even. The reason you're seeing 0.839999 is because it's not exactly 0.84, and the number prints to six significant figures.

In hex, which is just compacted binary, 0.84 is 0.D70A3D70A3D70A3.... It repeats forever. Floating point can handle it rounded off to 0.D70A3D.

If you want to display dollars and cents--keeping in mind you will only have 6-9 digits of precision--then you'll have to multiply the number by 100 and round. But that too could introduce new rounding errors. For absolute full precision you'll need a lib that can handle bigger numbers, possibly storing them as strings.
Just round to the hundreths place as part of any proc that affects the dollar var.
I need 100% precision up to 1 billion, so I guess I just have to build myself a lib that handles the math as strings, or make it so once you get so much it transfers to another currency, like World of Warcraft has copper/silver/gold, although I think they did this solely to prevent really large numbers once you amass quite a fortune.
Multiple currencies is probably your simplest option, as it would allow you fairly high precision since you can break it down into multiple numbers. A big number lib is really a lot of work. You don't even necessarily have to show those currencies as such in-game; you could use them strictly internally.

You can't have precision into the billions with single-precision floating point anyway. That would require double precision, which gives you 53 bits instead of 24. BYOND however doesn't support double precision, as it would expand the base Value type from 8 bytes (5 plus padding) to 16 and would require changing a lot of code around.