public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/47146] New: Floating point to integer conversions
@ 2011-01-01 15:41 babelart at yahoo dot com
  2011-01-01 18:21 ` [Bug c/47146] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: babelart at yahoo dot com @ 2011-01-01 15:41 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

           Summary: Floating point to integer conversions
           Product: gcc
           Version: 4.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: babelart@yahoo.com


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

//-----------------------------------------------------------------------------
int32_t main(int argc, char **argv)
//-----------------------------------------------------------------------------
{
    float elapsed = 0.3894949;

    fprintf( stdout, "Float 0.3894949=%f\n", elapsed );
    fprintf( stdout, "Float 0.3894949 * 100.0=%f\n", elapsed * 100.0 );
    fprintf( stdout, "Integer Cast 0.3894949 * 100.0=%d\n", (int32_t) (elapsed
* 100.0) );

    elapsed = 0.3894949 * 100;
    int32_t ielapsed = 0.3894949 * 100;

    fprintf( stdout, "Float=%f\n", elapsed );
    fprintf( stdout, "Integer=%d\n", ielapsed );

    fprintf( stdout, "------------------------------\n" );
    elapsed = 0.39;

    fprintf( stdout, "Float 0.39=%f\n", elapsed );
    fprintf( stdout, "Float 0.39 * 100.0=%f\n", elapsed * 100.0 );
    fprintf( stdout, "Integer Cast 0.39 * 100.0=%d\n", (int32_t) (elapsed *
100.0) );

    elapsed = 0.39 * 100;
    ielapsed = 0.39 * 100;

    fprintf( stdout, "Float=%f\n", elapsed );
    fprintf( stdout, "Integer=%d\n", ielapsed );

    exit( 0 );
}


The above code generates the following:

Float 0.3894949=0.389495
Float 0.3894949 * 100.0=38.949490
Integer Cast 0.3894949 * 100.0=38
Float=38.949490
Integer=38
------------------------------
Float 0.39=0.390000
Float 0.39 * 100.0=38.999999
Integer Cast 0.39 * 100.0=38
Float=39.000000
Integer=39

Casting and rounding seems to be a problem.

The compiler build options are:

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.4-6'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --with-tune=generic --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.4 (Debian 4.3.4-6) 


The system is;

Linux devel 2.6.30-2-686 #1 SMP Sat Sep 26 01:16:22 UTC 2009 i686 GNU/Linux

The processor is:

processor    : 0
vendor_id    : AuthenticAMD
cpu family    : 15
model        : 107
model name    : AMD Athlon(tm) 64 X2 Dual Core Processor 5200+
stepping    : 2
cpu MHz        : 2712.740
cache size    : 512 KB


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
@ 2011-01-01 18:21 ` redi at gcc dot gnu.org
  2011-01-01 21:15 ` kargl at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2011-01-01 18:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-01-01 18:21:01 UTC ---
(In reply to comment #0)
> Casting and rounding seems to be a problem.

Please be more specific.

Do you understand how floating point works?

Do you know that "0.39 * 100" has type double, not float, so has more precision
(and a different value) than "(float)0.39 * 100" ?

If you use doubles, or consistently use floats (not a mix of floats and
doubles) then you get consistent results


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
  2011-01-01 18:21 ` [Bug c/47146] " redi at gcc dot gnu.org
@ 2011-01-01 21:15 ` kargl at gcc dot gnu.org
  2011-01-03 17:12 ` babelart at yahoo dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: kargl at gcc dot gnu.org @ 2011-01-01 21:15 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |kargl at gcc dot gnu.org
         Resolution|                            |INVALID

--- Comment #2 from kargl at gcc dot gnu.org 2011-01-01 21:15:30 UTC ---
(In reply to comment #0)
> #include <stdio.h>
> #include <stdlib.h>
> 
> //-----------------------------------------------------------------------------
> int32_t main(int argc, char **argv)
> //-----------------------------------------------------------------------------
> {
>     float elapsed = 0.3894949;
> 
>     fprintf( stdout, "Float 0.3894949=%f\n", elapsed );
>     fprintf( stdout, "Float 0.3894949 * 100.0=%f\n", elapsed * 100.0 );
>     fprintf( stdout, "Integer Cast 0.3894949 * 100.0=%d\n", (int32_t) (elapsed
> * 100.0) );
> 

If you change all your floating point constants to include a f
suffix (e.g., 100.0f, 0.3894949f, etc), then you get

laptop:kargl[149] cc -o z a.c -ffloat-store && ./z
Float 0.3894949=0.389495
Float 0.3894949 * 100.0=38.949490
Integer Cast 0.3894949 * 100.0=38
Float=38.949490
Integer=38
------------------------------
Float 0.39=0.390000
Float 0.39 * 100.0=39.000000
Integer Cast 0.39 * 100.0=39
Float=39.000000
Integer=39

which looks like the desired output.  As Jonathan mentions, you
have several intermediate results that are double not float.
Add the fact that 0.39 is not exactly representable, which
your output of "Float 0.39 * 100.0=38.999999" clearly shows,
you've miss interpreted your results.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
  2011-01-01 18:21 ` [Bug c/47146] " redi at gcc dot gnu.org
  2011-01-01 21:15 ` kargl at gcc dot gnu.org
@ 2011-01-03 17:12 ` babelart at yahoo dot com
  2011-01-03 17:40 ` schwab@linux-m68k.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: babelart at yahoo dot com @ 2011-01-03 17:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

--- Comment #3 from Pierre Innocent <babelart at yahoo dot com> 2011-01-03 17:12:05 UTC ---
Dear kargl:

Sorry, I was not specific enough. It is the integer conversion that seem
to be wrong,for example the following two lines:

fprintf( stdout, "Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
fprintf( stdout, "Float 100 * 0.3894949=%d\n", 100 * elapsed );

both produce the value '-536870912'.

I also downloaded the C99 integer to float conversion test code; and they
generated two many failures. I also believe the compiler should round resulting
integer values when stripping decimals off.

Regards,
Pierre Innocent


--- On Sat, 1/1/11, kargl at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org> wrote:

> From: kargl at gcc dot gnu.org <gcc-bugzilla@gcc.gnu.org>
> Subject: [Bug c/47146] Floating point to integer conversions
> To: babelart@yahoo.com
> Received: Saturday, January 1, 2011, 4:15 PM
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
> 
> kargl at gcc dot gnu.org changed:
> 
>            What 
>   |Removed           
>          |Added
> ----------------------------------------------------------------------------
>          
>    Status|UNCONFIRMED     
>            |RESOLVED
>              
>    CC|         
>                
>   |kargl at gcc dot gnu.org
>          Resolution| 
>                
>           |INVALID
> 
> --- Comment #2 from kargl at gcc dot gnu.org 2011-01-01
> 21:15:30 UTC ---
> (In reply to comment #0)
> > #include <stdio.h>
> > #include <stdlib.h>
> > 
> >
> //-----------------------------------------------------------------------------
> > int32_t main(int argc, char **argv)
> >
> //-----------------------------------------------------------------------------
> > {
> >     float elapsed = 0.3894949;
> > 
> >     fprintf( stdout, "Float
> 0.3894949=%f\n", elapsed );
> >     fprintf( stdout, "Float
> 0.3894949 * 100.0=%f\n", elapsed * 100.0 );
> >     fprintf( stdout, "Integer Cast
> 0.3894949 * 100.0=%d\n", (int32_t) (elapsed
> > * 100.0) );
> > 
> 
> If you change all your floating point constants to include
> a f
> suffix (e.g., 100.0f, 0.3894949f, etc), then you get
> 
> laptop:kargl[149] cc -o z a.c -ffloat-store && ./z
> Float 0.3894949=0.389495
> Float 0.3894949 * 100.0=38.949490
> Integer Cast 0.3894949 * 100.0=38
> Float=38.949490
> Integer=38
> ------------------------------
> Float 0.39=0.390000
> Float 0.39 * 100.0=39.000000
> Integer Cast 0.39 * 100.0=39
> Float=39.000000
> Integer=39
> 
> which looks like the desired output.  As Jonathan
> mentions, you
> have several intermediate results that are double not
> float.
> Add the fact that 0.39 is not exactly representable, which
> your output of "Float 0.39 * 100.0=38.999999" clearly
> shows,
> you've miss interpreted your results.
> 
> -- 
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug.
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
                   ` (2 preceding siblings ...)
  2011-01-03 17:12 ` babelart at yahoo dot com
@ 2011-01-03 17:40 ` schwab@linux-m68k.org
  2011-01-03 18:01 ` sgk at troutmask dot apl.washington.edu
  2011-01-06  7:34 ` babelart at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: schwab@linux-m68k.org @ 2011-01-03 17:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

--- Comment #4 from Andreas Schwab <schwab@linux-m68k.org> 2011-01-03 17:40:24 UTC ---
> fprintf( stdout, "Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
> fprintf( stdout, "Float 100 * 0.3894949=%d\n", 100 * elapsed );
> 
> both produce the value '-536870912'.

Undefined behaviour: you pass a double value to a format that expects a value
of type int.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
                   ` (3 preceding siblings ...)
  2011-01-03 17:40 ` schwab@linux-m68k.org
@ 2011-01-03 18:01 ` sgk at troutmask dot apl.washington.edu
  2011-01-06  7:34 ` babelart at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2011-01-03 18:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

--- Comment #5 from Steve Kargl <sgk at troutmask dot apl.washington.edu> 2011-01-03 18:01:11 UTC ---
On Mon, Jan 03, 2011 at 05:12:10PM +0000, babelart at yahoo dot com wrote:
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
> 
> Sorry, I was not specific enough. It is the integer conversion that seem
> to be wrong,for example the following two lines:
> 
> fprintf( stdout, "Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
> fprintf( stdout, "Float 100 * 0.3894949=%d\n", 100 * elapsed );
> 
> both produce the value '-536870912'.
> 
> I also downloaded the C99 integer to float conversion test code; and they
> generated two many failures. I also believe the compiler should round resulting
> integer values when stripping decimals off.
> 
> Regards,
> Pierre Innocent
> 

Compile the code with -Wall and fix all the warnings.

#include <stdio.h>
int
main (void)
{
   float elapsed;
   elapsed = 0.3894949;  /* Note, the rhs is a double! */
   printf("Float 100.0 * 0.3894949=%d\n", 100.0 * elapsed );
   printf("Float 100 * 0.3894949=%d\n", 100 * elapsed );
   return 0;
}

troutmask:kargl[208] cc -o z -Wall a.c
a.c: In function 'main':
a.c:7: warning: format '%d' expects type 'int', but argument 2 has type
'double'
a.c:8: warning: format '%d' expects type 'int', but argument 2 has type
'double'

Your printf statements are using the first 4 bytes of
the 8 byte double argument.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/47146] Floating point to integer conversions
  2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
                   ` (4 preceding siblings ...)
  2011-01-03 18:01 ` sgk at troutmask dot apl.washington.edu
@ 2011-01-06  7:34 ` babelart at yahoo dot com
  5 siblings, 0 replies; 7+ messages in thread
From: babelart at yahoo dot com @ 2011-01-06  7:34 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146

--- Comment #6 from Pierre Innocent <babelart at yahoo dot com> 2011-01-06 03:17:01 UTC ---
Daer Kargl,

Thanks, my mistake !

The C99 tests failures may be due to non-athlon specific code.
Still checking !

Regards,
Pierre Innocent

--- On Mon, 1/3/11, sgk at troutmask dot apl.washington.edu
<gcc-bugzilla@gcc.gnu.org> wrote:

> From: sgk at troutmask dot apl.washington.edu <gcc-bugzilla@gcc.gnu.org>
> Subject: [Bug c/47146] Floating point to integer conversions
> To: babelart@yahoo.com
> Received: Monday, January 3, 2011, 1:01 PM
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
> 
> --- Comment #5 from Steve Kargl <sgk at troutmask dot
> apl.washington.edu> 2011-01-03 18:01:11 UTC ---
> On Mon, Jan 03, 2011 at 05:12:10PM +0000, babelart at yahoo
> dot com wrote:
> > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47146
> > 
> > Sorry, I was not specific enough. It is the integer
> conversion that seem
> > to be wrong,for example the following two lines:
> > 
> > fprintf( stdout, "Float 100.0 * 0.3894949=%d\n", 100.0
> * elapsed );
> > fprintf( stdout, "Float 100 * 0.3894949=%d\n", 100 *
> elapsed );
> > 
> > both produce the value '-536870912'.
> > 
> > I also downloaded the C99 integer to float conversion
> test code; and they
> > generated two many failures. I also believe the
> compiler should round resulting
> > integer values when stripping decimals off.
> > 
> > Regards,
> > Pierre Innocent
> > 
> 
> Compile the code with -Wall and fix all the warnings.
> 
> #include <stdio.h>
> int
> main (void)
> {
>    float elapsed;
>    elapsed = 0.3894949;  /* Note, the
> rhs is a double! */
>    printf("Float 100.0 * 0.3894949=%d\n",
> 100.0 * elapsed );
>    printf("Float 100 * 0.3894949=%d\n", 100
> * elapsed );
>    return 0;
> }
> 
> troutmask:kargl[208] cc -o z -Wall a.c
> a.c: In function 'main':
> a.c:7: warning: format '%d' expects type 'int', but
> argument 2 has type
> 'double'
> a.c:8: warning: format '%d' expects type 'int', but
> argument 2 has type
> 'double'
> 
> Your printf statements are using the first 4 bytes of
> the 8 byte double argument.
> 
> -- 
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug.
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-01-06  3:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-01 15:41 [Bug c/47146] New: Floating point to integer conversions babelart at yahoo dot com
2011-01-01 18:21 ` [Bug c/47146] " redi at gcc dot gnu.org
2011-01-01 21:15 ` kargl at gcc dot gnu.org
2011-01-03 17:12 ` babelart at yahoo dot com
2011-01-03 17:40 ` schwab@linux-m68k.org
2011-01-03 18:01 ` sgk at troutmask dot apl.washington.edu
2011-01-06  7:34 ` babelart at yahoo dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).