MAMEWorld >> The Loony Bin
View all threads Index   Threaded Mode Threaded  

Pages: 1

italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


Programming/maths question....soft floats...
#312003 - 07/26/13 02:21 PM


Torn in my head on how to handle an issue. I have many things to consider (timing, memory, volume of code additions, etc) that are jumbling the way I'm looking at this issue. Curious on how some of the more seasoned vets would tackle this one, or if someone would set up the math different.

The goal:

A simple ratio(ish) equation where I take points A, B and C in a data plot, and perform the equation "(A/C) / (B/C)". I need to keep at least two significant digits worth of precision.

example values might be 15034, 15044, 15157 (A,B,C)

The issue:

I'm working with an embedded processor that only does soft floats. Not only is this time consuming, but works out the correct answer only 75% of the time. Other times I see "rounding errors" (assuming they are rounding errors int he soft float libs) which slightly skew my two significant digits.

No matter how I play with the numbers, precision, ect, as long as floats are involved I get the occasional glitch. Thinking of adding fixed point libs, but that is going to be a pretty large hit for a tiny issue.

Thoughts, comments? I'm working on six different issues at the moment, and I can't seem to clear my head on this one...



GatKongModerator
Tetris Mason
Reged: 04/20/07
Posts: 5907
Loc: Sector 9
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312004 - 07/26/13 02:56 PM


For starters (A/C)/(B/C)=A/B







Gor
Giver of truth.
Reged: 09/21/03
Posts: 1925
Loc: The basement
Send PM


Re: Programming/maths question....soft floats... new [Re: GatKong]
#312007 - 07/26/13 03:52 PM


> For starters (A/C)/(B/C)=A/B




Oh for Pete's sake.
loser.com



italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


Re: Programming/maths question....soft floats... new [Re: Gor]
#312009 - 07/26/13 04:14 PM


> > For starters (A/C)/(B/C)=A/B

Okay okay....piss poor example aside, maybe i can clarify a little later when I get home on a real PC.

Trying to ask a specific question without giving up a specific code sample sucks..



Vas Crabb
BOFH
Reged: 12/13/05
Posts: 4466
Loc: Melbourne, Australia
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312016 - 07/26/13 06:31 PM


Fixed point isn't particularly hard to do as long as you have extended precision multiply/divide. Lacking that it's a bitch. As long as each argument has the same precision, add/subtract/compare are all just like doing the operation on integers. Multiply requires you to do an extended precision multiply, then adjust and truncate the result, and divide requires you to extend and adjust the dividend, then divide and truncate the result. It's easier and more reliable than bringing floats into it.



amused
MAME Fan
Reged: 04/24/08
Posts: 300
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312017 - 07/26/13 07:50 PM


As for keeping "significant digits": if you mean keeping decimal digits, you definitely should see this: http://floating-point-gui.de/

As for dividing without a division instruction: see this: http://www.math.usm.edu/lambers/mat460/fall09/lecture10.pdf

If you are working on integers, and want the result as a percentage, you don't need soft floats. You can compute (A/B) as ((100*A)/B) and have your answer as a percentage. If you want to round to the nearest percentage point, compute (A/B) as ((((200*A)/B)+1)/2) or maybe ((((200*A)/B)+1)>>1) where >> is binary right-shift.



lharms
MAME Fan
Reged: 01/07/06
Posts: 908
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312021 - 07/26/13 09:53 PM


If code size is a real consideration then I would say 'pick your battle'.

Find the bit that is destroying the most precision and then just use fixed point there and then convert it back.

My rule of thumb even with full floating point precision is 1 decimal of lost precision confidence per operation. Not always true but usually close enough.

I would probably 64bit int it (even if it cost a bunch more instructions/memory) shift it and then integer mul/div it then shift it back then drop it back into the float with a round so the rest of the code is ok.

Even that may not be 'ok' depending on how many operations you do. Pretty much for divides think about it as 1 number you cant trust anymore. For multiplies you need 2x the space. You can reduce the error by limiting your inputs. Instead of for example 2 8 bit vals multiplied together can in theory fill a 16 bit value. But by limiting them to 0-15 (4 bits) each you still fit in the 8 bit.



krick
Get Fuzzy
Reged: 02/09/04
Posts: 4235
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312029 - 07/26/13 10:55 PM


Some thoughts/questions...

Is there any way to use a lookup table to simplify the math?

Are the input values limited to specific values or a limited range?

What kind of rounding/truncation are you using? http://en.wikipedia.org/wiki/Rounding

My gut says that some sort of a custom fixed point implementation might be the best course of action. For example, when dealing with money, it's often easiest to work in cents, using an implied decimal point rather than working with dollars and fractional dollars.



GroovyMAME support forum on BYOAC



italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


Thanks for all the suggestions.... new [Re: italie]
#312034 - 07/27/13 12:44 AM


It sucks having to try and work through a difficult issue, without being able to just drop the actual code slice here for review. You guys got me thinking, and I think I have a viable stop-gap until some hardware changes are made in a few months.

The issue was indeed being caused by the IDE/embedded soft-float libs. Looks like they had some major issues fixed in recent versions. Pushed the math out to 64 bits and avoided float for the time. (This required me to alter a whole slew of external data, but it's the only way I'm getting anything close to reliable.)

Looks like the new hardware coming in will be a bit of an upgrade with an FPU...



italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


Re: Programming/maths question....soft floats... new [Re: amused]
#312035 - 07/27/13 12:50 AM


> As for keeping "significant digits": if you mean keeping decimal digits, you
> definitely should see this: http://floating-point-gui.de/
>
> As for dividing without a division instruction: see this:
> http://www.math.usm.edu/lambers/mat460/fall09/lecture10.pdf
>
> If you are working on integers, and want the result as a percentage, you don't need
> soft floats. You can compute (A/B) as ((100*A)/B) and have your answer as a
> percentage. If you want to round to the nearest percentage point, compute (A/B) as
> ((((200*A)/B)+1)/2) or maybe ((((200*A)/B)+1)>>1) where >> is binary right-shift.

Just curious for my own betterment, was significant digits not a term that carried across well?



krick
Get Fuzzy
Reged: 02/09/04
Posts: 4235
Send PM


Re: Programming/maths question....soft floats... new [Re: italie]
#312039 - 07/27/13 04:52 AM


> Just curious for my own betterment, was significant digits not a term that carried
> across well?

It's a complicated subject and not everyone speaks the same language...

http://en.wikipedia.org/wiki/Significant_figures



GroovyMAME support forum on BYOAC


Pages: 1

MAMEWorld >> The Loony Bin
View all threads Index   Threaded Mode Threaded  

Extra information Permissions
Moderator:  GatKong 
0 registered and 462 anonymous users are browsing this forum.
You cannot start new topics
You cannot reply to topics
HTML is enabled
UBBCode is enabled
Thread views: 1079