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.

DarkBASIC Discussion / Real Number problems, using a floating variable to handle cash

Author
Message
Zaxtor99
9
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 25th Sep 2011 19:23 Edited at: 25th Sep 2011 19:53
Okay, making a game trying to use a floating variable Money# to handle cash.

My problem is displaying the variable correctly when the value of this variable is an even dime or dollar (such as $10.9 for $10.90 or $10 for $10.00)

Right now I am using the following lines of text to place one or two additional zeros of text to compensate:



If i set the money# variable to $100 at the beginning of the program, that code works correctly.

But if I subtract another money variable, say food# which I set to 1.99 (food#=1.99) and I use the same lines of code, after it is subtracted ten times.. or twenty or any other number of times which my above code should fix, it doesn't fix it. For example 100-1.99 ten times would be 80.1 which my above code SHOULD fix to add an extra "0" to to make it display 80.10.

So to figure out what's going on, I wrote a quick program to display to the screen what's going on here:


from that program, it's obvious that when I set a floating variable at a certain value directly, it sees that r#*10=int(r#*10), but after reducing that variable, even though I display both as being exactly the same value, the program doesn't see them as the same...

My question is WHY and what is the best way to resolve this problem??

Thank you!

- Zaxx

OKAY UPDATE with a POTENTIAL SOLUTION, but I don't understand why this works or the above code doesn't work (Code Insert #2)

Here is my work around:


Notice the changes I made was to make F# a regular variable instead of "F=199" vs the old "F#=1.99"
- Then I simply multiplied my Money# floating variable by 100 before subtracting F ten times in the for/next loop, followed by dividing my money# variable by 100 after the for/next loop.

Why this works and why the code insert #2 above doesn't is beyond me.. I would love an explanation and any advice on better code suggestions if you have them.

Thanks again!

- Zaxx
zenassem
17
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 25th Sep 2011 22:32 Edited at: 25th Sep 2011 22:37
I don't use DBC anymore, but some of this is the same with dbpro. It's really all about the fact that the language doesn't directly let you set the precision of floats. DBpro at least (and i assume DBC) does a good job in loosely setting the precion to the variable with the largest decimal places,, but in instances of where it ends in a 0, it truncates that precision. So we wind up with values like 80.1 rather than 80.10,, which is critical in a money program.

Don't use decimals. Simply keep everything muliplied by 100. Than write a routine that converts the value to a string and adds the "." in the string (before the last two characters) for displaying purposes.

IanM had a little snippet he wrote in [DBpro]. Perhaps it will help you in doing something similar in dbc

Quote: "
Switch to double floats, or use integers/double integers and simulate decimal places.
"




Your signature has been erased by a mod please reduce it to 600 x 120.
Silverman
13
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 26th Sep 2011 00:21 Edited at: 26th Sep 2011 01:00
hi Zaxtor99,


it's for DBC:




EDIT: 23h58, corrected a minor mistake, new code.

@+

DirectX 9.0c (February 2010)/ DBClassic v1.13
Zaxtor99
9
Years of Service
User Offline
Joined: 27th Jul 2011
Location: Boise, Idaho, USA
Posted: 26th Sep 2011 00:43 Edited at: 26th Sep 2011 01:49
Yes thanks for your input guys!

After zenassem posted I went and wrote this little routine to print
my cash to the screen:



I've changed my program to handle my money with a standard variable vs the floater. So instead of money#=100.00, I now have money=10000 at the beginning of my game. And I use this code insert to read the value of my money variable and print it to the screen correctly. Notice it makes my money output to the screen look correct even when the value is reduced to less then 100 (less then $1.00) and less then 10 cents as well.

ER is simply a variable before I call my routine to erase the old
dollar value on the screen (if ER=1) before displaying it after updating it by setting ER to 0.

I like your snipet as well Silverman and may use it to make my code neater and more efficient.

Thanks for your help guys!! I was about to pull my hair out trying to work with the decimals and the calculations with the floating variables just making no sense (and cents, lol)

- Zaxx
zenassem
17
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 26th Sep 2011 11:47
Your welcome Zaxtor99. Nice work with the routine you coded!!!

btw, I spent some time out in your parts. I was stationed at Mountain Home AFB for 3 years from 94-98. Had a great time out there. I was a whitewater rafting boatsman for the Outdoor Adventure Program for 1 year. Enjoyed the lazy afternoons tubing on the Boise River along the green-belt. Joe's All American & Joe's Down Under saw a lot of my paycheck. Live after 5's (downtown square) & the Balloon Festival made the summers rock. Go Broncos!!!!

Your signature has been erased by a mod please reduce it to 600 x 120.

Login to post a reply

Server time is: 2020-08-15 11:46:55
Your offset time is: 2020-08-15 11:46:55