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 / Float Issue

Author
Message
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th May 2012 10:26
I was going over some old code and found a decimal to fraction converter. It has some bugs so I re-wrote it but I'm still having issues. I don't understand why numbers >=1.0 aren't working properly.

Displaying the f# variable shows that it sometimes gets given incorrect values from a simple subtraction. Is there something I'm missing about how numbers are stored that is producing this strange behaviour?

WARNING: The above comment may contain sarcasm.
Pincho Paxton
17
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 15th May 2012 11:33
Well I converted it to DBPro to save me installing DBC. What I found was that converting b to f# wasn't working because both DBPro, and DBC don't like cross referencing fractions, and decimals. This code doesn't work either, but it is close, and you might see a way to fix it...



Pincho Paxton
17
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 15th May 2012 11:36
Or if you turn everything into fractions you get this, which might be what you wanted...



Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th May 2012 13:38
Thanks but that doesn't work either. I hate DB right now!

WARNING: The above comment may contain sarcasm.
Latch
14
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th May 2012 21:52
You may want to wade through this post regarding numbers in general in DBC:

http://forum.thegamecreators.com/?m=forum_view&t=124455&b=10

There's a particular snippet I wrote that pretty much sums up the inaccuracy of floats and the resulting inaccuracy in float conversions with functions such as val() and str$():



Enjoy your day.
Silverman
13
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 17th May 2012 12:39 Edited at: 18th May 2012 23:33
@OBese87
if you use the method of the fixed point (with strings, very accurate), the highest number will be 2147483646.999999999 (you see, 9 digits after the decimal point, with dbc!). Then use the Greatest common divisor and the modulo operation to reduce your fraction.



@Pincho Paxton
for b# = 1.0 to 10.0
print b#
next b#
this does not work in DBC




Edit: add snippet

DirectX 9.0c (February 2010)/ DBClassic v1.20
Diggsey
14
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 26th May 2012 00:15
You should never use exact comparison with floating pointer numbers, you should check if they are within a small range of each other instead.

Fixed version:


[b]
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 27th May 2012 00:59 Edited at: 27th May 2012 02:06
Thanks, I didn't know that but it makes sense now I think about it.
There's no "floor" command in DBC so I wrote my own rounding function, this way it works for negative values too.


I was wondering how to write this as a function, how would I return a single value? I decided to return a string and found a useful side-effect of the val() command: it returns the numbers up to the first non-numerical character, in our case the numerator of our fraction. So converting a string back to a decimal is fairly simple, I just have to grab the denominator (length of the entire string - length of the numerator - one to drop the slash). Of course this means I will have to allow top-heavy fractions as a space would mess up the val() conversion. This is fine though, it's a more flexible way of doing it and you can remove the integer, prior to conversion, quite simply by n#-int(n#). (Sometimes top-heavy fractions are even preferred.)

[edit: code updated]

WARNING: The above comment may contain sarcasm.
Fluffy Rabbit
User Banned
Posted: 27th May 2012 22:12
My take on your function:


Just my 2 cents. Also, why would we want to highlight DarkBASIC's shortcomings, in the days of AppGameKit and a free version of DBPro? I hope we don't lose too many community members.
Libervurto
14
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th May 2012 05:26
I only still use DBC because of DBPs short-comings and terrible editor, so it's swings and roundabouts. (I've never understood that saying, how are swings and roundabouts similar?)

Login to post a reply

Server time is: 2020-08-05 03:30:25
Your offset time is: 2020-08-05 03:30:25