Re: MicroBlaze floating point precision issues



Thank you, John, for the quick response.

On Jan 6, 10:29 pm, John Williams <jwilli...@xxxxxxxxxxxxxx> wrote:
Hi,



JD Newcomb wrote:
I'm having a heck of a time trying to figure out why my MicroBlaze
implementation is presenting *supposedly* single-precision floating
point values as double-precision values truncated to 32 bits.

Here's what I mean:

float test = 22; // for example

printf("test = 0x%08x\r\n",test); // prints "test = 0x40360000"
printf("test = %d\r\n",test); // prints "test = 1077280768"
printf("test = %f\r\n",test); // prints "test = 22.000000" or
some precision

22 in single-point floating point (hex) is 0x41B00000, and in double-
precision is 0x4036000000000000. So, truncate the double-precision
value and you have what's printed to the screen. I'm completely
baffled. Am I missing something? A gcc flag? How is this even possible
for a single-precision instruction set?

If that literally is your test code, GCC might be being a bit too clever
for you and effectively hard coding the literal '22' as the arg to the
printf statements.


Well, the above wasn't the code that got me investigating the problem
in the first place, but the results are the same.

Try using

float test = 22.0f;

as the initial assignment for starters. To force single precision
constants you need the 'f' suffix, otherwise you burn tragic number of
cycles doing wasteful double to float conversions.

I'm not using constants in my code that pertains to this particular
issue. I have a function that returns an int, and I typecast it to a
float and store it in the float variable:

float foo;

foo = (float) bar(&tmp); // tmp is some 32-bit ASCII-like value
that's converted to an integer value... don't worry about it.

bar() in this case would return 22 as an int, and printing the
variable foo results in the same printed statement as my test code
above.


Also, perhaps specify optimisation level -O0 to gcc, see if that gets
you anywhere.

Regards,

John

No optimizations also get me the same result. Any other ideas?
Compiler flags I'm not seeing? The other question is: is this even
wrong? I mean, I say it is because calculating the values out by hand
using the IEEE 754 standard shows that the results are wrong. But
using a value like 450.123f in the test code above printed as the 32-
bit truncated double-precision value as 0x40776000 (single-precision
is actually 0x43BB0000). Assuming that the truncation would reduce its
precision, the floating value printed out as 450.122986. [shrug] Maybe
it is right.
.



Relevant Pages

  • Re: Weird problem on Lahey lf95 v6.2b
    ... >>print statement, but in the second and third has been truncated down ... > of register usage - enough to flush most anything. ... use of extended precision for intermediate results, ... The x87 processor has the ability to truncate the fraction ...
    (comp.lang.fortran)
  • Re: Problem with qromb subroutine/nrerror
    ... single-precision variables all over the place when your core function ... Also you define double precision constants ... where it's defined (should be nrtype) and your whole program changes ...
    (comp.lang.fortran)
  • Re: Using a GPU for supercomputing?
    ... GPU, because the floating point is single-precision only, and HPC ... players because of the double precision issue. ...
    (comp.sys.ibm.pc.hardware.chips)
  • Re: "typedef" in F95/2003?
    ... quad double types from the Hida/Bailey package, there exists a defined assignment operator for this combination. ... you'll have just lost precision. ... Your "foo" variable, on the other hand, as a quad-double, would normally expected to have a maximum numerical error that is substantially smaller than the error in a single-precision real variable. ...
    (comp.lang.fortran)
  • Re: TRUNC Function
    ... them into degrees minutes and seconds (i.e truncate the ... then the 5th one rounds to 30 instead of 29. ... I have tested it using a calculator and I can see how it ... not held to its full precision in the calculator memory. ...
    (microsoft.public.excel.misc)