f



0.1 + 0.1 + 0.1 - 0.3

  This is gcc 4.8.1 with no compiler option used:

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

int main( void )
{ double x = rand() < 0 ? 0 : 0.1;
  double y = rand() < 0 ? 0 : 0.3;
  printf( "%g\n", x + x + x - y ); 
  printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }

2.77556e-017
5.55112e-017

  The second number written becomes �0� as soon as I add
  the compiler option �-std=c11�, why?

0
ram
10/24/2014 2:41:07 AM
comp.lang.c 30657 articles. 4 followers. spinoza1111 (3246) is leader. Post Follow

8 Replies
1714 Views

Similar Articles

[PageSpeed] 59

On 10/23/14 9:41 PM, Stefan Ram wrote:
>    This is gcc 4.8.1 with no compiler option used:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main( void )
> { double x = rand() < 0 ? 0 : 0.1;
>    double y = rand() < 0 ? 0 : 0.3;
>    printf( "%g\n", x + x + x - y );
>    printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }
>
> 2.77556e-017
> 5.55112e-017
>
>    The second number written becomes �0� as soon as I add
>    the compiler option �-std=c11�, why?

Early adopter syndrome?

gcc 4.2.1:

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

int main( void )
{ double x = rand() < 0 ? 0 : 0.1;
   double y = rand() < 0 ? 0 : 0.3;
   printf( "%g\n", x + x + x - y );
   printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 );
}

~/Desktop: gcc sr.c -o sr
~/Desktop: sr
5.55112e-17
5.55112e-17

:-)

-- 
Morris Dovey
http://www.iedu.com/Solar/
http://www.facebook.com/MorrisDovey
0
Morris
10/24/2014 3:00:16 AM
On 2014-10-24, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>   This is gcc 4.8.1 with no compiler option used:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main( void )
> { double x = rand() < 0 ? 0 : 0.1;
>   double y = rand() < 0 ? 0 : 0.3;
>   printf( "%g\n", x + x + x - y ); 
>   printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }
>
> 2.77556e-017
> 5.55112e-017
>
>   The second number written becomes »0« as soon as I add
>   the compiler option »-std=c11«, why?

Might it be that the concern I alluded to in my earlier posting,
that the type is actually "long double" is actually real?

Your response was that surely that kind of obvious problem would be discovered.

But would it?

How many programs in a Linux distro use printf to print floating-point
*constants*? And of those who do, which have a regression test which
checks that output? Of those who do, does anyone run it when rolling
the distro? ;)

With the following program:

  #include <stdio.h>
  #include <string.h>

  int main(void)
  {
    long double x = 0.1L + 0.1L + 0.1L - 0.3L;
    double y;
    memcpy(&y, &x, sizeof y);
    printf("%g\n", y);
    printf("%g\n", 0.1 + 0.1 + 0.1 - 0.3);
    return 0;
  }

I get:

  0
  5.55112e-17

It looks like indeed a long double is being passed to printf due to that
gcc bug, and the conversion specifier is taking the lower 8 bytes of
it (like my UB-inducing memcpy). That lower 8 bytes looks like a zero,
evidently.

Let's get the hex representation of it:

  #include <stdio.h>
  #include <string.h>

  int main(void)
  {
    long double x = 0.1L + 0.1L + 0.1L - 0.3L;
    fwrite(&x, sizeof x, 1, stdout);
    return 0;
  }

  $ ./longdouble | od -t x1
  0000000 00 00 00 00 00 00 00 00 00 00 00 00
  0000014

Why, it's all zero; long double is precise enough that the calculation cancels
to zero.
0
Kaz
10/24/2014 3:20:38 AM
ram@zedat.fu-berlin.de (Stefan Ram) wrote:
>    This is gcc 4.8.1 with no compiler option used:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main( void )
> { double x = rand() < 0 ? 0 : 0.1;
>    double y = rand() < 0 ? 0 : 0.3;
>    printf( "%g\n", x + x + x - y );
>    printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }
>
> 2.77556e-017
> 5.55112e-017
>
>    The second number written becomes �0� as soon as I add
>    the compiler option �-std=c11�, why?
>

http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html

--
Les Cargill
0
Les
10/24/2014 3:35:59 AM
Stefan Ram writes:
> 2.77556e-017
> 5.55112e-017

The difference in these two values is one binary
bit of mantissa encoding. It could be a rounding
issue of compile-time vs. runtime computation.

The 0.0 value can only come from applied
rounding at 15 or 16 significant digits in base
10 being identified as 0.0 for all practical
purposes due to the exponent (unlikely), or by
extra precision in computation, which still
should not yield 0.0, but also some tiny value.

What does the disassembly show for those
lines? That will tell you the difference in the
compiler flag code, and why it varies.

CodeLite's GUI gives graphical access to GDB
output. It makes it easy to get disasm, and data
if that helps. http://www.codelite.org.

Best regards,
Rick C. Hodgin
0
Rick
10/24/2014 7:09:56 AM
On 24 Oct 2014 02:41:07 GMT, (Stefan Ram) wrote:

>  This is gcc 4.8.1 with no compiler option used:
>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main( void )
>{ double x = rand() < 0 ? 0 : 0.1;
>  double y = rand() < 0 ? 0 : 0.3;
>  printf( "%g\n", x + x + x - y ); 
>  printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }
>
>2.77556e-017
>5.55112e-017
>
>  The second number written becomes �0� as soon as I add
>  the compiler option �-std=c11�, why?

i think compiler in "�-std=c11�" is more accurate for find immediate
result

it seems the little difference here is 
"x + x + x - y" is computed from fpu
and 0.1 + 0.1 + 0.1 - 0.3 is computed from the compiler as immediate
data
#include <stdio.h>
#include <stdlib.h>

int main(void)
{ double  x=0.1, y=0.3;
  printf("x=%g y=%g\n", x, y);
  printf( "%g\n", x + x + x - y ); 
  printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); 
  return  0;
}
 
0
Rosario193
10/24/2014 7:46:58 AM
On Friday, October 24, 2014 3:10:07 AM UTC-4, Rick C. Hodgin wrote:
> What does the disassembly show for those
> lines? That will tell you the difference in the
> compiler flag code, and why it varies.

// double x = rand() < 0 ? 0 : 0.1;
0x0000000000400585  call   0x400480 <rand@plt>
0x000000000040058a  test   eax,eax
0x000000000040058c  jns    0x400595 <main+24>
0x000000000040058e  mov    eax,0x0
0x0000000000400593  jmp    0x40059f <main+34>
0x0000000000400595  movabs rax,0x3fb999999999999a      ; 64-bit double 0.1
0x000000000040059f  mov    QWORD PTR [rbp-0x10],rax

// double y = rand() < 0 ? 0 : 0.3;
0x00000000004005a3  call   0x400480 <rand@plt>
0x00000000004005a8  test   eax,eax
0x00000000004005aa  jns    0x4005b3 <main+54>
0x00000000004005ac  mov    eax,0x0
0x00000000004005b1  jmp    0x4005bd <main+64>
0x00000000004005b3  movabs rax,0x3fd3333333333333      ; 64-bit double 0.3
0x00000000004005bd  mov    QWORD PTR [rbp-0x8],rax

// printf( "%g\n", x + x + x - y );
0x00000000004005c1  movsd  xmm0,QWORD PTR [rbp-0x10]
0x00000000004005c6  addsd  xmm0,xmm0
0x00000000004005ca  addsd  xmm0,QWORD PTR [rbp-0x10]
0x00000000004005cf  subsd  xmm0,QWORD PTR [rbp-0x8]

Note:  the value in xmm0 at this point is 5.5511151231257827e-17
(per GDB output showing xmm0 register contents as doubles)

0x00000000004005d4  mov    edi,0x400694
0x00000000004005d9  mov    eax,0x1
0x00000000004005de  call   0x400450 <printf@plt>

// printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 );
0x00000000004005e3  movabs rax,0x3c90000000000000      ; 64-bit double

Note:  This value was the computed value at runtime.

0x00000000004005ed  mov    QWORD PTR [rbp-0x18],rax
0x00000000004005f1  movsd  xmm0,QWORD PTR [rbp-0x18]
0x00000000004005f6  mov    edi,0x400694
0x00000000004005fb  mov    eax,0x1
0x0000000000400600  call   0x400450 <printf@plt>

[rbp-0x10] and [rpb-0x8]
0x00007fffffffe5c0: 0x9a 0x99 0x99 0x99 0x99 0x99 0xb9 0x3f  : ???????? 0.1
0x00007fffffffe5c8: 0x33 0x33 0x33 0x33 0x33 0x33 0xd3 0x3f  : 333333?? 0.3

I believe you're seeing the difference in rounding  between compile-time
computation of double data stored in [rpb-0x18], compared to the computed
value at runtime of double data stored in memory locations [rbp-0x10] (0.1)
and [rbp-0x8] (0.3).

-----
As for the -std=c11 flag, on my machine the output was always the same.
I did not show a variation between rounding modes, and all outputs were
always 5.55112e-017.

AMD 1055T 6-core x64 machine running Linux Mint 17, GCC 4.8.1, CodeLite
6.1 interface to GDB 7.7.

Best regards,
Rick C. Hodgin
0
Rick
10/24/2014 7:57:17 AM
On Friday, October 24, 2014 3:57:39 AM UTC-4, Rick C. Hodgin wrote:
> // printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 );
> 0x00000000004005e3  movabs rax,0x3c90000000000000      ; 64-bit double
> 
> Note:  This value was the computed value at runtime.
>
> 0x00000000004005ed  mov    QWORD PTR [rbp-0x18],rax
> 0x00000000004005f1  movsd  xmm0,QWORD PTR [rbp-0x18]
> 0x00000000004005f6  mov    edi,0x400694
> 0x00000000004005fb  mov    eax,0x1
> 0x0000000000400600  call   0x400450 <printf@plt>

Correct.  Should read:

    "Note:  This value was the computed value at compile-time."

Best regards,
Rick C. Hodgin
0
Rick
10/24/2014 8:01:03 AM
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>  This is gcc 4.8.1 with no compiler option used:
 
> #include <stdio.h>
> #include <stdlib.h>
 
> int main( void )
> { double x = rand() < 0 ? 0 : 0.1;
>  double y = rand() < 0 ? 0 : 0.3;
>  printf( "%g\n", x + x + x - y ); 
>  printf( "%g\n", 0.1 + 0.1 + 0.1 - 0.3 ); }
 
> 2.77556e-017
> 5.55112e-017

Another example of the general rule not to compare floating point
values for equality, even when you believe that they are computed
the same way.
 
>  The second number written becomes �0� as soon as I add
>  the compiler option �-std=c11�, why?

There are too many different ways to compute it.

In the old days, the x87 floating point kept values in 80 bit
(64 significant bits) registers. When rounded to double, they might
generate an exact zero. Or not.

Exactly why it changes with C11, is hard to say.

-- glen

0
glen
10/24/2014 10:18:01 AM
Reply:

Similar Artilces:

1 - 1, 1 -1, 1-1, 1
Hi, I have a question regarding lexical analysis. I recently came across a bug in our lexical analyser in phc (www.phpcompiler.org), that I am unsure how to solve. This is the problem: our current definition for integer constant looks something like INT ([1-9][0-9]*)|0 In particular, note that it does not allow for an (optional) "+" or "-" at the start of the integer. This means that the strings "1 - 1", "1 -1" and "1-1" all generate the same sequence of three tokens INT(1), OP(-), INT(1), for which the syntax analyser generates the subtr...

1.1.1.1 ?
hi my firewall logs dropped packets from an internal IP address trying to contact 1.1.1.1 through port 9999. Any ideas whether 1.1.1.1 is valid IP? and what is port 9999?? thanks mike wrote: > hi > > my firewall logs dropped packets from an internal IP address trying to > contact 1.1.1.1 through port 9999. Any ideas whether 1.1.1.1 is valid > IP? and what is port 9999?? > thanks These trojans *BlitzNet*, *Backdoor.Oracle*, *Backdoor.Spadeace* uses port 9999 -- S.S. "StarScripter" <Star@privacy.net> wrote in message news:<bv8ejj$p54t3$1@ID-185702.new...

plot 1, -1, 1, -1, -1
Hi, How to plot those numbers in a rectangle style? thanks x = linspace(-4*pi,4*pi,1000); y = sin(x); plot(x,sign(y)), axis([-12 12 -2 2]) PZ <patrick.zou@gmail.com> wrote in message <d64c545f-f2ac-4f87-af89-051bda896707@x6g2000vbg.googlegroups.com>... > Hi, > > How to plot those numbers in a rectangle style? > > thanks help stairs stairs([-1 1 -1 1 -1 1]) hth Jos ...

how can i write this statement labels=[1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;2;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;]; in compress fo
How to write it in compact form to avoid repetition "shah " <shahkhn3@gmail.com> wrote in message <lodugh$8hr$1@newscl01ah.mathworks.com>... > How to write it in compact form to avoid repetition You'll find in this thread different solutions for similar question http://www.mathworks.fr/matlabcentral/newsreader/view_thread/335814 Bruno ...

OO 1.1 and java 1.3.1
Am attempting to install OO 1.1, but with just Java 1.3.1, and the install documentation says that without at least 1.4.1, I cannot make use of full capabilties. I infer from this that I should be able to OO crippled. However, However, OO does not like it when I point to my 131 JRE. Comments? -- Sir: David I. Smith wrote: > Am attempting to install OO 1.1, but with just Java 1.3.1, and the > install documentation says that without at least 1.4.1, I cannot make > use of full capabilties. I infer from this that I should be able to > OO crippled. However, However, OO...

[1 1 1 1 1 ;2 2 2 2 2 ;3 3 3 3 3 3;....;n n n n n]
Hi, Anybody knows how to create this matrix but without using any loops? a=[1 1 1 1 1 ;2 2 2 2 2 ;3 3 3 3 3 ;.......;n n n n n ] Thank you. Hana. Hana wrote: > Hi, > Anybody knows how to create this matrix but without using any loops? > a=[1 1 1 1 1 ;2 2 2 2 2 ;3 3 3 3 3 ;.......;n n n n n ] HELP REPMAT - Randy Hana wrote: > > > Hi, > Anybody knows how to create this matrix but without using any > loops? > a=[1 1 1 1 1 ;2 2 2 2 2 ;3 3 3 3 3 ;.......;n n n n n ] > > Thank you. > Hana. Hope this isn't homework. >> repmat([1:n]'...

Quickeo 1.1.1.3
Quickeo is for people who want to share their digital photos, videos and music with their friends, family and community - not the whole world. Quickeo is private file-sharing with a simple email. Quickeo lets you create rich multimedia newsletters in a matter of minutes. Quickeo is the only sharing service that lets you create a rich multimedia gallery (of videos, audios, images and other files) and choose exactly who may view each gallery through email. Quickeo allows you to present your multimedia content in a newsletter or album format, with video introduction (can be captured in ...

JDK 1.3.1/1.4/1.5
How can you display JDialog without the Close on the Top Right in any of these versions(JDK 1.3.1/1.4/1.5). The only thing it displays is the close button X and I don't want that? Thank you! vnssoftware wrote: > How can you display JDialog without the Close on the Top Right in any > of these versions(JDK 1.3.1/1.4/1.5). The only thing it displays is > the close button X and I don't want that? > > Thank you! Dialog.setUndecorated() since 1.4 -- Knute Johnson email s/nospam/knute/ Molon labe... ...

1.1, 1.2, 1.3 are all goners now
It seems, well, it doesn't seem but it is, well, it seems that the subject version of Workbenches won't be any more these, versions of Workbench, but versions of Amix. The Amiga's go for the Network. :-)) Have you seen that CDTV on amiga.jorg? That kind od schalametry for games, like some high tech heavily classic games will be played... Heyyyaaa!!! p.s. some piece of equipment, eh.. CD, floppy, installable HD, all, joysticks, joypads for CDTV, playing CDs and playing games. It's like you'll need a high class massive wood shelf for this, the CDs and for the - :( r...

ActivePython 3.1.1.2 vs Python 3.1.1 for OSX?
I am just curious which I should use. I am going to start learning Python soon. Are they comparable and I just do a "eenie meenie minie moe"? Bob On 1 Oct, 00:51, Robert Hicks <sigz...@gmail.com> wrote: > I am just curious which I should use. I am going to start learning > Python soon. Are they comparable and I just do a "eenie meenie minie > moe"? > > Bob First off, a great choice of language to begin trying! Is it your first language (I'm guessing not), or do you come from another 'background'. Basically, Active is a possible 'supe...

Lomboz 3.1.1 plugin not seen in Eclipse 3.1.1
I am facing the same problem. Here are the details OS Windows 2000 JDK 1.42.04 Eclipse 3.1.1 EMF 2.1.0 JEM 1.1 Lomboz 3.1.1 I see 'org.objectweb.lomboz 1.0.0' when I go to Product Configuration. I can enable and disable it without any issues but I do not see the Lomboz options when I go to Windows>Customize Prespective. I tried installing it several times doing it differently every time after reading some posts over the web but none of it has helped. If some one can suggest something that will be of great help. Thanks in advance. On 17 Oct 2005 18:58:06 -0700, surenm@gmail.com...

SQLObject 1.3.3 and 1.4.1
Hello! I'm pleased to announce bugfix releases 1.3.3 and 1.4.1. What's new in SQLObject ======================= * Fixed bugs in pickling and unpickling (remove/restore a weak proxy to self, fixed cache handling). * Added an example of using SQLObject with web.py to the links page. Contributors for this release are Andrew Trusty and Rhubarb Sin. For a more complete list, please see the news: http://sqlobject.org/News.html What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and ro...

SQLObject 1.3.3 and 1.4.1
Hello! I'm pleased to announce bugfix releases 1.3.3 and 1.4.1. What's new in SQLObject ======================= * Fixed bugs in pickling and unpickling (remove/restore a weak proxy to self, fixed cache handling). * Added an example of using SQLObject with web.py to the links page. Contributors for this release are Andrew Trusty and Rhubarb Sin. For a more complete list, please see the news: http://sqlobject.org/News.html What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and ro...

Ip-Address 1.1.1.1
A traceroute to 213.176.224.4 showed following results: # traceroute -In -m10 213.176.224.4 traceroute to 213.176.224.4 (213.176.224.4), 10 hops max, 38 byte packets 1 212.152.136.1 103.658 ms 99.452 ms 109.767 ms 2 212.152.151.2 99.646 ms 99.680 ms 99.821 ms 3 62.218.1.93 99.689 ms 109.665 ms 99.751 ms 4 212.152.192.182 99.762 ms 1359.690 ms 99.718 ms 5 193.203.0.72 104.398 ms 105.014 ms 109.781 ms 6 146.188.2.229 109.696 ms 109.733 ms 99.771 ms 7 146.188.14.113 119.710 ms 199.720 ms 209.720 ms 8 146.188.49.194 119.695 ms 129.663 ms 119.808 ...

ERROR IN EXPRESSION; -1 - (-1) not 1
The expression isn't a = 1 - (1-), but: a = -1 - (1) der <der@noemail.com> scribbled the following: > The expression isn't a = 1 - (1-), but: a = -1 - (1) Ah, that's different, then. Then the answer is: The result will be -2. You have *no* guarantees about which of -1 or (1) is evaluated first, but as they don't have side effects, you don't have to care. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\ | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ ...

Web resources about - 0.1 + 0.1 + 0.1 - 0.3 - comp.lang.c

Key Moments from the Democratic Town Hall Meeting in Iowa
With just seven days remaining before the Iowa caucuses, the three Democratic candidates gathered in Des Moines for a "town hall" forum hosted ...

Michigan AG Appointed Prosecutor to Lead Flint Water Probe
Michigan's attorney general has named a former prosecutor to spearhead an investigation into the process that left Flint's drinking water tainted ...

Pair Behind Planned Parenthood Videos Indicted in Texas
Two anti-abortion activists who made undercover videos alleging that Planned Parenthood illegally sold fetal tissue to researchers for a profit ...

Couple casually takes stunning engagement photos in record-setting blizzard
For one Northeastern couple, buckets full of snow close to their wedding day is not so much a nightmare, but a dream come true Over the stormy ...

Leading Electric Car Models & Leading Companies
In our 4th article pulled from Electric Cars: What Early Adopters & First Followers Want — a new report from CleanTechnica, EV Obsession, and ...

Watch India T20 Cricket Vs. Australia Live Online: Streaming Link, 1st Match Of World Cup Prep Series ...
Fans of the India cricket team will want to watch the first T20 match against Australia streaming live online Tuesday, as their country gets ...

Meet Twitter's new product chief: Jack Dorsey
If you've been wondering who will replace Twitter's departing head of product, Kevin Weil, now you have your answer. Nobody. Or, put another ...

Refurbished Apple TV 4 Now Available on Apple Online Store
Apple has added refurbished Apple TV 4 models to its online store for refurbished products, less than a week after refurbished 12-inch Retina ...

Peyton Manning's Son Marshall Stole the Show at His Championship Press Conference
Peyton Manning ‘s four-year-old son Marshall stole the show at his post AFC Championship game press conference held at Sports Authority Field ...

‘Batman V. Superman’: Darkseid Teased, Zack Snyder On Doomsday, And Lois Lane’s ‘Big Scene’ with Batman ...
The hype for the Batman v. Superman: Dawn of Justice movie is gaining. A lot of news has come out regarding the caped crusader and the last son ...

Resources last updated: 1/26/2016 9:42:21 AM