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 Professional Discussion / How precise are floats?

Author
Message
sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 04:09
I've seen some threads about the accuracy of floats, but for what I'm working on I'm more concerned with how precise they can be. Basically, what I'm looking for is how many decimal places out can they still be considered accurate?

I've done some testing, such as making a float equal to 263.647 and printing a string with it just to find an extra 5 or 6 decimal places after that. What causes that?

Also, is there a method to where I can get even more precision than floats? I'm looks for about 4 decimal places.

Thanks

If at first you dont succeed, LOWER YOUR STANDARDS.
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Oct 2010 04:41 Edited at: 8th Oct 2010 04:47
Googled "accuracy of floats"

From Wikipedia
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

If a float doesn't provide enough accuracy, use a double float.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 8th Oct 2010 05:24 Edited at: 8th Oct 2010 05:24
I believe floating point numbers provide about 7 digits accuracy.
I mean, If you type in "a#=263.6471"
and when you print that value out, you get something like
263.647068999
that's still really close.

The reason is because floating point numbers are basically stored in exponential notation. The mantissa is the coefficient (IE in 1.123*10^20, 1.123 is the mantissa), and the number is stored in something along the lines of
mantissa*2^(exponent). I think the mantissa is 24 bits, and the exponent is 8 bits.

I'm just trying to give you a general idea of how a float works, so don't quote me


[edit]
also double floats in DBPro don't work.

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 05:25 Edited at: 8th Oct 2010 05:43
Quote: " use a double float"


Is their anything at the end of a variable for this (ie variable# for float)?

EDIT:
Also, I went ahead and made all the double floats but now I'm getting the following error: "The type of FOR statement values must match the type of the variable"

EDIT2:
Also, 7 digits is close to the value, but I'm analyzing data from nanoseconds

If at first you dont succeed, LOWER YOUR STANDARDS.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 8th Oct 2010 05:41
[read my edit or go to the bug reports or do a forum search for double floats]

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 06:05
Quote: "also double floats in DBPro don't work."


I looked through the forums, and I saw the confirmed bug report. I just wanted to ask though what doesn't work about them? is it they are treated as just floats?

If this is the case I guess I'm at a standstill till it gets fixed.

If at first you dont succeed, LOWER YOUR STANDARDS.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 8th Oct 2010 09:40
Post your problem you have, I'm sure there's a work around...

TheComet

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 15:43
Quote: "Post your problem you have, I'm sure there's a work around..."


It's been posted . If you download the code you will see all the calculations there with the floats. I'm just looking for as much precision as possible is all.

http://forum.thegamecreators.com/?m=forum_view&t=176371&b=1

If at first you dont succeed, LOWER YOUR STANDARDS.
kaedroho
18
Years of Service
User Offline
Joined: 21st Aug 2007
Location: Oxford,UK
Posted: 8th Oct 2010 16:13
Their accuracy changes depending on how high the number is. Floats are made up with a 23 bit number 1 bit sign, and a 8 bit exponent. The numbers are accurate to 23 bits but the range can be shifted to 256 different levels.

For example, If you have a number which is 2^23 floats will no longer be able to do decimal numbers as every bit would be used to get the number that big. If the number is only 2^10, then it only needs 10 bits to make the number that big and the other 13 bits can be used for the decimal.

If you have a number which is less than 1 it will use all 23 bits for the decimal.

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 18:43
@Neuro Fuzzy or kaedroho or anyone else that can help.
So 23 bits represents 7 decimal places then if my math is correct (2^23). And of those 7 decimal places only represents about 84% of the numbers? If so how can I get closer to a 100% of the numbers?

If at first you dont succeed, LOWER YOUR STANDARDS.
KISTech
18
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Oct 2010 19:23
Quote: "also double floats in DBPro don't work."


Hmm.. Didn't know that. Then again, I haven't had a reason to use them yet.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Oct 2010 21:05 Edited at: 8th Oct 2010 21:05
Quote: "If so how can I get closer to a 100% of the numbers?"

You don't. You can't.

A float is made from 32 bits of storage - that's 4,294,967,296 unique combinations of values. That number is spread over a non-zero/non-negative range of 3.4x10^-38 (nearest representation to zero, excluding zero itself) and 3.4x10^38 (nearest representation to infinity, excluding infinity itself).

To deal with the big difference between these two ranges, the floats have been designed with logarithmic scale of accuracy, specifying accuracy based on significant digits rather than decimal places.

In actuality, some of those combinations of 32 bits are used to represent various versions of infinity (or overflow really) and NAN (not a number), and zero (positive and negative), so those soak up some of the possible range too.

Double floats (even if they worked fully in DBPro) only move the problem away from you a little - they work on exactly the same concept, except on 64 bits instead of 32.

... Not that any of that helps you in any way to increase the accuracy of your calculations

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 8th Oct 2010 23:32
Quote: "Double floats (even if they worked fully in DBPro) only move the problem away from you a little"


Moving the problem away would help.

Quote: "Not that any of that helps you in any way to increase the accuracy of your calculations"


But at least I have a better understanding of what error I'm working with. Also... so it's representing numbers up to 4 Billion -ish... this means that it technically only represents 43% of the possible numbers, correct? (4294967296/9999999999)

Also, one more last thing to IanM. Is there any plans and/or timeline to improve Double Float functionality?

If at first you dont succeed, LOWER YOUR STANDARDS.
dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 9th Oct 2010 05:20 Edited at: 9th Oct 2010 05:21
What is this 9999999999 number? And what do you mean 100% of the numbers? There's an infinite amount of numbers between any two real values, so nothing can represent 100% of them.

In your OP you stated you wanted 4 DP of accuracy, if this is all you need then use fixed point arithmetic, whereby you use integers to do your calculations and when printing the values out you simply insert a decimal point when writing the numbers out(though this won't work beyond simple operations). Another way is to store two integer values, one for the integer part and one for the decimal, but this makes calculations a little more tricky. Though I'm sure you can find many resources online that tell you exactly how to handle this.

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 9th Oct 2010 07:15
On cellphone, so can't quote, but in order that they were asked:
1. 9999999999 would be the largest 10 digit number.
2. Meant 4 more in OP sorry. Can you give an example though for future reference?

If at first you dont succeed, LOWER YOUR STANDARDS.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Oct 2010 13:10 Edited at: 9th Oct 2010 13:11
Quote: "1. 9999999999 would be the largest 10 digit number."


And what is the relevance of that exactly?

I think you need to be more specific about why you need this extra accuracy. If it is essential then simply state your needs precisely and work out the arithmetic necessary to achieve that. I'm sure some variation of Dark Coder's suggestion will work - when we know what precisely is needed.

Quote: "Meant 4 more in OP sorry."


The number of decimal places is irrelevant as Dark Coder and others have pointed out. The important thing is the overall range and desired accuracy of numbers that you need to work with. Also, why on earth do you need perfect accuracy of a number such as 263.6475347 (i.e. your original example plus 4 more decimal places)? Once we know why then someone can probably help you with the details.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Oct 2010 14:52
@sneaky smith12,
To state it more explicitly, floats give an approximation of your numbers to 7 significant digits, and have a floating decimal point (hence the name of the type).

From the wikipedia page:
Quote: "a floating-point representation (such as the IEEE 754 decimal32 format) with seven decimal digits could in addition represent 1.234567, 123456.7, 0.00001234567, 1234567000000000, and so on. The floating-point format needs slightly more storage (to encode the position of the radix point), so when stored in the same space, floating-point numbers achieve their greater range at the expense of precision."


Note the last part from the comma in particular.

If you want fixed-point calculations (you state 4 decimal places, not significant digits), then you can use the double integer type to give you 4 implied decimals, and 10 extra digits in front of the decimal point (taking into account digit shifting needed for multiplication/division).

It'll be slow, and ugly, but it's doable.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
Lost in Thought
22
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 9th Oct 2010 15:11
Yeah you could easily setup your own math routines using arrays of integers. It's slow but dead accurate to how ever many places you want on either side of the decimal. You say 4 places after the decimal, but what's the max number of places on the left side of the decimal you'll need?

Is the speed of the calcs an issue? I could write you something to do it if not, but it may be a few days. I am installing a new HDD in my laptop hopefully tonight and I gotta get everything loaded back and setup again. I'll have a look at your code as soon as I can.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Oct 2010 22:19
Quote: "You say 4 places after the decimal, but what's the max number of places on the left side of the decimal you'll need?"

This is very approximate, but for every decimal place you have, you lose 1 extra digit on the left if you are carrying out multiplication or division, unless you take extra care and carry out extra processing. That's why I said 4 + 10, not 4 + 14 - double integers hold a guaranteed 18 digits.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 10th Oct 2010 05:54
Ok, so on cellphone again, but here I go.

I was told that the project can't lose significant digits, and I've seen at most 7, but haven't checked all 9miilion some numbers. The project is a subproject for Cresis (cresis.ku.edu). We're measuring the effects of birefringence in the ice pact of Antarctica. I already proved the current software we have(gnuplot) inevitably loses significant digits, so I'm trying to fix it, and fix it for good.

The speed isn't as important in the long run because it's really only going to be used in a very quick computer at the university.

Sorry, it took me so long for this explanation, and I hope this is the info you need. It's not the math that's my problem I'm just trying to better understand the data types, so I can document it accurately.

If at first you dont succeed, LOWER YOUR STANDARDS.
gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 11th Oct 2010 23:54 Edited at: 11th Oct 2010 23:56
If you really need up to 7 significant digits to remain accurate, you're really gonna have to write your own math routines to handle floating point numbers and their operations. As stated, the IEEE 754 floating point standard (what DBP, and virtually every piece of hardware or software used today for personal computers, uses) really is not going to be accurate enough for this kind of precision.



Even with something as simple as 0.1+0.2, you see how you start to get weird results after your 7th digit, which is already pushing what you're looking for as far as precision is concerned - Once you start going into more in depth calculations, you can't guarantee you'll be within acceptable accuracy or not.

Bottom line - If you really need the accuracy, you need to write your own methods to handle these operations and the storage of these numbers. Any programming language (DBP or not) that uses "standard" floating point numbers will be using the IEEE standard, which will give you these same results.

While it's not impossible to program something like that in DBP, I'd say an object-oriented language would definitely be your best bet for something of this sort. If you really need the DBP functions (if you're showing 3D graphical representations of things, for example) then I'd say look into DarkGDK. Otherwise, my two recommendations would be straight C++ or Java, but there are of course plenty of object-oriented languages to choose from.
sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 12th Oct 2010 00:07
Quote: "Otherwise, my two recommendations would be straight C++ or Java, but there are of course plenty of object-oriented languages to choose from."


Thanks, and I'll see what I can do. I was afraid of this answer, but figured that's what eventually I'd get told. I know basically no C++ but am not beyond learning

If at first you dont succeed, LOWER YOUR STANDARDS.

Login to post a reply

Server time is: 2026-07-22 12:41:44
Your offset time is: 2026-07-22 12:41:44