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.

Newcomers DBPro Corner / Floating Points Won't Behave?

Author
Message
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 17th Nov 2012 21:07
Okay, I always have issues like this:

I want a float to be a number that has EXACTLY two decimals below it. I always want it to display this way. I don't want it to show even a thousands, and I want numbers that don't have decimals to still display ".00" at the end. This can be accomplished using Matrix1Utils by some variant of PAD STRING RIGHT, how I'm not sure.



After holding UP long enough, I eventually got "23.3999".
In math class, this behavior is not permitted. In my game economy, I don't want to run serious risk of my numbers bugging out and outputting nonsense values like that.

And YES, I do need to implement serious floating-point related calculations. For example, price increases of 105%. I then need to round down or up to the nearest hundredth, as the situation permits.

Is there an actual fix for this? Why won't floats behave?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Nov 2012 00:43


"You're not going crazy. You're going sane in a crazy world!" ~Tick
Brendy boy
19
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 18th Nov 2012 04:08
str$(value, number of decimal places)

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Nov 2012 05:09
woh, when did that become a command?

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 18th Nov 2012 19:55
Well, since float math never seems to cooperate and that bugs me to no end, I have used your code, Phaelax, to round everything properly. It turns it into a string temporarily, but it comes back as a float with any number of decimals I can now do my math with FLOATS. Never will displays have odd numbers at the end again.

Hopefully. Fingers crossed.

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 18th Nov 2012 23:23
Brendy Boy's advice is the better solution.

TheComet

- The codebase
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 18th Nov 2012 23:36
Not necessarily.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 18th Nov 2012 23:37
How so? It does it much faster.

TheComet

- The codebase
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 19th Nov 2012 01:48
Quote: "How so? "

it doesn't round or include accurate decimal places but simply int()'s the value# and adds 0's

Virtual Nomad @ California, USA . DBPro V7.7
AMD Phenom™ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 20th Nov 2012 01:20
Quote: "it doesn't round or include accurate decimal places but simply int()'s the value# and adds 0's"


Exactly. I didn't elaborate in the first place because you didn't.

Imagine, under the original system, my final calculated price, due to all the crazy modifiers it was under, was 22.4783. Just showing the bottom two, it would display as "$22.47".
Now, if the player had $22.47 EXACTLY. Due to the way it's calculated currently, the actual number would be something like 22.4703.

It would SAY the player didn't have enough money to buy the item, even though, for all he can see, he does.
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 21st Nov 2012 21:45
Floating point values simply are not accurate by definition.
All values they hold are approximations represented using a mantissa and an exponent.

If you just want it for displaying output (ie. strings) the given solutions should probably be good enough.


"Why do programmers get Halloween and Christmas mixed up?"
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 22nd Nov 2012 23:37
Quote: "Floating point values simply are not accurate by definition.
All values they hold are approximations represented using a mantissa and an exponent."


I was thinking that was probably the case, though I know very little about how they work, bitwise.

I wonder, then, why do the returns suddenly store reliably once you round them off? O.o

I think what I'll likely end up doing is using the system I described for fronting calculations that MUST use floats. The currency system I will probably just store all the cash values in cents instead of dollars, and round off, as described in the original method. The additional division needed when using percentile applications seems like a minor cost for more accurate math solutions.

Thanks to Rudolpho and all others who contributed.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 26th Nov 2012 15:05 Edited at: 26th Nov 2012 15:06
If I were you I would discard floats for cash; just store cents rather than dollars (pence rather than pounds, copper rather than gold; no need to deal with 1/2 of one cent.

Store them as integers; DBPRO's fastest data type for calculations.

When you need to display the currency in text; divide by 100.0 (decimal point crucial to cast as float), or multiply by 0.01; multiplication is faster 90% of the time (in DBPRO on my machine)


So 19999 cents / 100.0 is $199.99.
123451 * 0.01 is $1234.51.



Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 26th Nov 2012 17:01
Quote: "If I were you I would discard floats for cash; just store cents rather than dollars (pence rather than pounds, copper rather than gold; no need to deal with 1/2 of one cent."


Cool... cool... kinda just said that... but yeah...

Quote: "multiplication is faster 90% of the time (in DBPRO on my machine)"


(Nonsarcastically) thanks. I'll keep that in mind.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Nov 2012 00:20
Quote: "The currency system I will probably just store all the cash values in cents instead of dollars, and round off, as described in the original method."


lol, how did I not see... , what is happening to me... why brain no function

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Nov 2012 01:20
Quote: "what is happening to me... why brain no function"


You must be running a nightly build. Better backup and use a stable version.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 27th Nov 2012 03:08


Yeah, the nightly Chris Tate builds always have serious string processing bugs for some reason. I think it has to do with pointers or something else in the memory cache.

Anyhow, Phaelax, I shot you an email. I ran into a tutorial that coincidentally happened to be written by you having a little trouble.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Nov 2012 09:26
Got it last night, I've just replied to it.

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2024-11-21 18:37:52
Your offset time is: 2024-11-21 18:37:52