All Forums
 Microsoft Windows CE
 eVC Application Development
 Weird result with floating point calculation.
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Author Topic  

jjmackay

24 Posts

Posted - 13 Feb 2014 :  13:21:25  Show Profile  Email Poster
I have a calculation that give two different result on a PC and on a GCX. The result on the GCX is really strange.

On the GCX
(those are all stored in float variable)
float ParkTime;
float CostPerUnit;
float SecondsInHour;
float TotalCost = ParkTime*(CostPerUnit/SecondsInHour);

printf("ParkTime: %f. CostPerUnit: %f. SecondsInHour: %f. TotalCost: %f.", ParkTime, CostPerUnit, SecondsInHour, TotalCost);

ParkTime: 3600.000000 CostPerUnit:60.000000 SecondsInHour: 3600.000000. TotalCost: 60.000004.

On a PC, it gives 60.000000; which is my expactation.

The code is compiled using VS 2005 and am using those options (set using the Project settings):
/ Op and /fp:strict

I get this warning while I compile:
"cl : Command line warning D9002 : ignoring unknown option '/Op'"

I have also tried to use
#pragma float_control(precise, on)
#pragma fenv_access(on)
#pragma float_control(except, on)

before function that uses the float variable.

I understand the imprecision of floating point number, but is there a way to control it?

akidder

1519 Posts

Posted - 13 Feb 2014 :  13:27:24  Show Profile  Email Poster
In this case, printing with %0.2f would probably meet the needs of the application. But this level of precision *is* a bit low.
Go to Top of Page

beitman

63 Posts

Posted - 13 Feb 2014 :  13:29:02  Show Profile  Email Poster
A couple of quick suggestions:

1. Try using double
2. Change the calculation to either (but probably a is better):
a. TotalCost = (ParkTime* CostPerUnit)/SecondsInHour;
b. TotalCost = (ParkTime/SecondsInHour) * CostPerUnit;
Go to Top of Page

jjmackay

24 Posts

Posted - 13 Feb 2014 :  14:06:32  Show Profile  Email Poster
Using double instead of float did work.

Thank you for this.

Are you able to provide an explanation or a technical document that would explain that difference on the GCX. This would help for us to determines that what extends with need to rerun a set of test.

Thanks,
Go to Top of Page

beitman

63 Posts

Posted - 13 Feb 2014 :  14:18:31  Show Profile  Email Poster
I suspect that the difference in data size between your PC and the GCX is where the problem lies. That, and the PXA255 does not have a floating point processor.

This is further complicated by the order of operations in the calculation of TotalCost. By first dividing, which results in 0.01666666666666666666666666, then multiplying you end up with the "weird" result. I find that these results can almost always be changed by changing the order of operations and/or the data size.

Is there a document that compares and contrast the differences between a PXA255 and your x86? No, probably not.
Go to Top of Page
  Topic  
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Jump To:
Eurotech Support Forums © Eurotech Inc. Go To Top Of Page
This page was generated in 0.03 seconds. Snitz Forums 2000