Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Computing > Arch Arithmetic > Rounding of res...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 285 of 366
Post > Topic >>

Rounding of results in conversion of IEEE floats to integers

by Dan Kelly <knivesandstabbingweapons@[EMAIL PROTECTED] > Dec 18, 2006 at 11:55 PM

Greetings all,

I am converting some floating point numbers to (signed or unsigned) 
integers I am not getting the expected results. It is perhaps the result 
of the C libraries/compiler that I am using. I have reproduced the same 
results on Intel and AMD processors.

As I understand, floating point conversion should be provided as a 
fundamental operation in an IEEE compliant FP implementation. 
Furthermore, in conversions the rounding schemes below should apply in 
the same way that they are applied to the result of one of the 
operations (+,-,*,/).

The code below has been compiled with gcc on linux platforms, using the 
GNU C libraries.

===================================================================
Code
----
/*
  * A program to print the conversions between fp and integer numbers.
  */

#include <fenv.h>
#include <stdio.h>
#include <stdlib.h>

main (void) {

   float float_a=1.6,float_b=1.4,float_c=1.2;
   int rnd,direct_conv,add_conv;
   char *rnd_str = malloc(8);

   for (rnd=0;rnd<4;rnd++) {

     /* set the rounding mode */
     if (rnd==0) {
       fesetround(FE_TONEAREST);
       rnd_str = "TO NEAR";
     } else if (rnd==1) {
       fesetround(FE_TOWARDZERO);
       rnd_str = "TO ZERO";
     } else if (rnd==2) {
       fesetround(FE_UPWARD);
       rnd_str = "TO +INF";
     } else if (rnd==3) {
       fesetround(FE_DOWNWARD);
       rnd_str = "TO -INF";
     }

     /* convert floats to integers */
     direct_conv = (int)float_a;
     add_conv = (int)(float_b + float_c);

     printf("\n| Round mode (%s)\n| ------------------------\n",rnd_str);
     printf("|         float     -> int\n");
     printf("| direct: %1.1f       -> %i\n",float_a,direct_conv);
     printf("| add   : %1.1f + %1.1f -> %i\n\n",
	   float_b,float_c,add_conv);

   }
}

===================================================================
Output*
------

| Round mode (TO NEAR)
| ------------------------
|         float     -> int
| direct: 1.6       -> 1    [2]#
| add   : 1.4 + 1.2 -> 2    {3,2}


| Round mode (TO ZERO)
| ------------------------
|         float     -> int
| direct: 1.6       -> 1    [1]
| add   : 1.4 + 1.2 -> 2    {2,2}


| Round mode (TO +INF)
| ------------------------
|         float     -> int
| direct: 1.6       -> 1    [2]#
| add   : 1.4 + 1.2 -> 2    {3,4}


| Round mode (TO -INF)
| ------------------------
|         float     -> int
| direct: 1.6       -> 1    [1]
| add   : 1.4 + 1.2 -> 2    {2,2}

===================================================================

* I have added my expected results in brackets to the right of the 
calculated result. Differing results are marked with a pound sign (#).
[direct-conversion]
{add-then-convert,convert-then-add}

Now, I am primarily concerned only with the direct conversion case, I 
have included the add-and-convert-result case just as an additional
anomaly.

Looking at the results above, it would appear that the rounding mode is 
being ignored in all cases, and the default "rounding" action is merely 
to truncate results.
1.6 (base10) = 1.1001100110011... (base2)

So, are the (#) results different to that expected because of my 
understanding IEEE 754, something to do with the GNU C library, the 
compiler, or the processor?

Thanks,

- Dan
 




 5 Posts in Topic:
Rounding of results in conversion of IEEE floats to integers
Dan Kelly <knivesandst  2006-12-18 23:55:52 
Re: Rounding of results in conversion of IEEE floats to integers
"Dik T. Winter"  2006-12-18 14:18:40 
Re: Conversion of IEEE floats to integers and intel/amd64
Andrew Reilly <andrew-  2006-12-19 22:51:55 
Re: Conversion of IEEE floats to integers and intel/amd64
"Dik T. Winter"  2006-12-20 00:16:51 
Re: Conversion of IEEE floats to integers and intel/amd64
Andrew Reilly <andrew-  2006-12-20 05:16:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Aug 29 22:44:16 CDT 2008.