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.

AppGameKit Classic Chat / accurate floating point maths request

Author
Message
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 27th Apr 2017 22:57
I've had a couple of really good app opportunities over the last few weeks that I've had to pass up because the floating point accuracy in AppGameKit is too low. I'd REALLY like to see a set of "Big Number" commands added. I don't care if they are really slow to process as they would not be required to real-time calculations, but just used in circumstances where the occasional accurate calculations are required.

For example: 332.156 / 10.0 does not = 33.215599 which is the answer AppGameKit comes up with.

I know this is all down to single precision floating point maths, but a slow big-numbers library would be great for the odd calc where accuracy is required.

Maybe someone has already written one? *fingers crossed*



Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 28th Apr 2017 00:19
I think Paul said that the ARM processors have trouble with double precision.
The coffee is lovely dark and deep,and I have code to write before I sleep.
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 28th Apr 2017 15:02
It is true that double precision would be slower, but with the move to 64bit ARM devices this will probably become less of a concern. However double precision will still have some rounding problems, it can go up to about 15 significant figures as opposed to single precision's 7 significant figures. If you need perfect floating point calculations then something like Binary Coded Decimals may be more suitable, however we have no plans to add that to AGK. It's possible someone could write a plugin that does it using strings to hold the operands and results.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 28th Apr 2017 15:14
Yeah. No double floats in the FPU, but that doesn't mean we can't write functions to deal with really big numbers, or massive floats with loads of decimal places.... They'll be slower than if all handled in hardware on the FPU, but meh. The goal for these commands would be accuracy, not speed. It would mean we could write a whole plethora of niche calculation apps. It IS the AppGameKit after all
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Apr 2017 15:30
Some time ago in a far away land known as DBP, I wrote a function that used strings to do addition of extremely large integers. Modifying it for decimals shouldn't be difficult but I'd still have to write a function to handle division and the other operations. Point is, it is doable.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 2nd May 2017 00:45
I believe your problem is related to the binary nature of floating-point numbers. Increasing the floating point precision may not help you. You want something like Binary Coded Decimals (BCD). See the book "Code" by Petzold, page 338.
The thing is that there are, as always, space limitations. Plus I don't think BCD is explicitly contemplated in most computer languages. Probably because all you need is to take into account the bias of floating-point numbers, and round your results.

332.156 / 10.0~33.215599

If you round the last digit of the result you get what you want because 9 goes up to 10 and that will give you 60. Try it. Single precision is accurate up to 1 part in 2^24, and double precision is accurate up to 1 part in 16,777,216.

Hope that helps!


Login to post a reply

Server time is: 2024-11-24 16:57:21
Your offset time is: 2024-11-24 16:57:21