Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Unexpected truncation behavior (http://www.velocityreviews.com/forums/t962254-re-unexpected-truncation-behavior.html)

Keith Thompson 06-28-2013 03:51 PM

Re: Unexpected truncation behavior
 
Giuseppe <giuseppe@somewhere.sw> writes:
> I tried to understand why I can't get the message:
>
> "r1 is 10, r2 is 10"
>
> as result from the code below when I compile it with gcc4.8.1 *on Windows*.
> What I get is:
>
> "r1 is 10, r2 is 9"
>
> instead. Any other compiler and gcc on Linux display the expected message.
> Wouldn't the C language "require" the result to be the one I expect?
>
> #include <stdio.h>
> int main(void)
> {
> float a = 3.3f, b = 3.3f, c = 3.4f;
> double d = 3.3, e = 3.3, f = 3.4;
> int r1, r2;
> r1 = a + b + c;
> r2 = d + e + f;
> printf("r1 is %d, r2 is %d\n", r1, r2);
> return 0;
> }


Here's a variant of your program that shows more precision. The
behavior of printf with extra precision is likely to vary from one
implementation to another; GNU libc shows the exact value in this case.

#include <stdio.h>
int main(void)
{
float a = 3.3f, b = 3.3f, c = 3.4f;
double d = 3.3, e = 3.3, f = 3.4;
int r1, r2;
r1 = a + b + c;
r2 = d + e + f;
printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);
printf("a + b + c = %.24f\n", a + b + c);
printf("d + e + f = %.24f\n", d + e + f);
printf("r1 is %d, r2 is %d\n", r1, r2);
return 0;
}

There's the output I get on Linux, x86_64, gcc 4.7.2:

a = 3.299999952316284179687500
b = 3.299999952316284179687500
c = 3.400000095367431640625000
d = 3.299999952316284179687500000000000000000000000000 00
e = 3.299999952316284179687500000000000000000000000000 00
f = 3.400000095367431640625000000000000000000000000000 00
a + b + c = 10.000000000000000000000000
d + e + f = 10.000000000000000000000000
r1 is 10, r2 is 10

On Windows 7, Cygwin, gcc 4.5.3 (Windows is 64 bits, but Cygwin uses 32
bits):

a = 3.299999952316284179687500
b = 3.299999952316284179687500
c = 3.400000095367431640625000
d = 3.299999952316284179687500000000000000000000000000 00
e = 3.299999952316284179687500000000000000000000000000 00
f = 3.400000095367431640625000000000000000000000000000 00
a + b + c = 10.000000000000000000000000
d + e + f = 10.000000000000000000000000
r1 is 10, r2 is 9

On Windws/Cygwin, the output varies with optimization level.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Lew Pitcher 06-28-2013 03:55 PM

Re: Unexpected truncation behavior
 
On Friday 28 June 2013 11:51, in comp.lang.c, kst-u@mib.org wrote:

> Giuseppe <giuseppe@somewhere.sw> writes:
>> I tried to understand why I can't get the message:
>>
>> "r1 is 10, r2 is 10"
>>
>> as result from the code below when I compile it with gcc4.8.1 *on
>> Windows*. What I get is:
>>
>> "r1 is 10, r2 is 9"
>>
>> instead. Any other compiler and gcc on Linux display the expected
>> message. Wouldn't the C language "require" the result to be the one I
>> expect?
>>
>> #include <stdio.h>
>> int main(void)
>> {
>> float a = 3.3f, b = 3.3f, c = 3.4f;
>> double d = 3.3, e = 3.3, f = 3.4;
>> int r1, r2;
>> r1 = a + b + c;
>> r2 = d + e + f;
>> printf("r1 is %d, r2 is %d\n", r1, r2);
>> return 0;
>> }

>
> Here's a variant of your program that shows more precision. The
> behavior of printf with extra precision is likely to vary from one
> implementation to another; GNU libc shows the exact value in this case.
>
> #include <stdio.h>
> int main(void)
> {
> float a = 3.3f, b = 3.3f, c = 3.4f;
> double d = 3.3, e = 3.3, f = 3.4;
> int r1, r2;
> r1 = a + b + c;
> r2 = d + e + f;
> printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
> printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);


Shouldn't the above line be
printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
= = =
?


> printf("a + b + c = %.24f\n", a + b + c);
> printf("d + e + f = %.24f\n", d + e + f);
> printf("r1 is %d, r2 is %d\n", r1, r2);
> return 0;
> }
>
> There's the output I get on Linux, x86_64, gcc 4.7.2:
>
> a = 3.299999952316284179687500
> b = 3.299999952316284179687500
> c = 3.400000095367431640625000
> d = 3.299999952316284179687500000000000000000000000000 00
> e = 3.299999952316284179687500000000000000000000000000 00
> f = 3.400000095367431640625000000000000000000000000000 00
> a + b + c = 10.000000000000000000000000
> d + e + f = 10.000000000000000000000000
> r1 is 10, r2 is 10
>
> On Windows 7, Cygwin, gcc 4.5.3 (Windows is 64 bits, but Cygwin uses 32
> bits):
>
> a = 3.299999952316284179687500
> b = 3.299999952316284179687500
> c = 3.400000095367431640625000
> d = 3.299999952316284179687500000000000000000000000000 00
> e = 3.299999952316284179687500000000000000000000000000 00
> f = 3.400000095367431640625000000000000000000000000000 00
> a + b + c = 10.000000000000000000000000
> d + e + f = 10.000000000000000000000000
> r1 is 10, r2 is 9
>
> On Windws/Cygwin, the output varies with optimization level.
>
> --
> Keith Thompson (The_Other_Keith) kst-u@mib.org
> <http://www.ghoti.net/~kst> Working, but not speaking, for JetHead
> Development, Inc.
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"


--
Lew Pitcher
"In Skills, We Trust"

Lew Pitcher 06-28-2013 04:10 PM

Re: Unexpected truncation behavior
 
On Friday 28 June 2013 11:55, in comp.lang.c, lew.pitcher@digitalfreehold.ca
wrote:

> On Friday 28 June 2013 11:51, in comp.lang.c, kst-u@mib.org wrote:

[snip]
>> Here's a variant of your program that shows more precision. The
>> behavior of printf with extra precision is likely to vary from one
>> implementation to another; GNU libc shows the exact value in this case.
>>
>> #include <stdio.h>
>> int main(void)
>> {
>> float a = 3.3f, b = 3.3f, c = 3.4f;
>> double d = 3.3, e = 3.3, f = 3.4;
>> int r1, r2;
>> r1 = a + b + c;
>> r2 = d + e + f;
>> printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
>> printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);

>
> Shouldn't the above line be
> printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
> = = =
> ?
>
>
>> printf("a + b + c = %.24f\n", a + b + c);
>> printf("d + e + f = %.24f\n", d + e + f);
>> printf("r1 is %d, r2 is %d\n", r1, r2);
>> return 0;
>> }
>>
>> There's the output I get on Linux, x86_64, gcc 4.7.2:
>>
>> a = 3.299999952316284179687500
>> b = 3.299999952316284179687500
>> c = 3.400000095367431640625000
>> d = 3.299999952316284179687500000000000000000000000000 00
>> e = 3.299999952316284179687500000000000000000000000000 00
>> f = 3.400000095367431640625000000000000000000000000000 00
>> a + b + c = 10.000000000000000000000000
>> d + e + f = 10.000000000000000000000000
>> r1 is 10, r2 is 10



Here's the output I get with your original code on Linux, x86_32, gcc 4.2.4

a = 3.299999952316284179687500
b = 3.299999952316284179687500
c = 3.400000095367431640625000
d = 3.299999952316284179687500000000000000000000000000 00
e = 3.299999952316284179687500000000000000000000000000 00
f = 3.400000095367431640625000000000000000000000000000 00
a + b + c = 10.000000000000000000000000
d + e + f = 10.000000000000000000000000
r1 is 10, r2 is 9

Here's the output I get with the corrected code

a = 3.299999952316284179687500
b = 3.299999952316284179687500
c = 3.400000095367431640625000
d = 3.299999999999999822364316059974953532218933105468 75
e = 3.299999999999999822364316059974953532218933105468 75
f = 3.399999999999999911182158029987476766109466552734 38
a + b + c = 10.000000000000000000000000
d + e + f = 10.000000000000000000000000
r1 is 10, r2 is 9


Here is the corrected code...

#include <stdio.h>
int main(void)
{
float a = 3.3f, b = 3.3f, c = 3.4f;
double d = 3.3, e = 3.3, f = 3.4;
int r1, r2;
r1 = a + b + c;
r2 = d + e + f;
printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
printf("a + b + c = %.24f\n", a + b + c);
printf("d + e + f = %.24f\n", d + e + f);
printf("r1 is %d, r2 is %d\n", r1, r2);
return 0;
}

HTH
--
Lew Pitcher
"In Skills, We Trust"

Keith Thompson 06-28-2013 05:41 PM

Re: Unexpected truncation behavior
 
Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
> On Friday 28 June 2013 11:51, in comp.lang.c, kst-u@mib.org wrote:

[...]
>> #include <stdio.h>
>> int main(void)
>> {
>> float a = 3.3f, b = 3.3f, c = 3.4f;
>> double d = 3.3, e = 3.3, f = 3.4;
>> int r1, r2;
>> r1 = a + b + c;
>> r2 = d + e + f;
>> printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
>> printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);

>
> Shouldn't the above line be
> printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
> = = =
> ?


Yes, absolutely, thanks for noticing that. (I was wondering why I
wasn't getting many more digits for double; I should have investigated
that).

With that fix, I get this output on Linux x86_64:

a = 3.299999952316284179687500
b = 3.299999952316284179687500
c = 3.400000095367431640625000
d = 3.299999999999999822364316059974953532218933105468 75
e = 3.299999999999999822364316059974953532218933105468 75
f = 3.399999999999999911182158029987476766109466552734 38
a + b + c = 10.000000000000000000000000
d + e + f = 10.000000000000000000000000
r1 is 10, r2 is 10

I don't have access to my Windows machine at the moment; I'll check that
later.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 04:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.