Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Unexpected truncation behavior

Reply
Thread Tools

Re: Unexpected truncation behavior

 
 
Keith Thompson
Guest
Posts: n/a
 
      06-28-2013
Giuseppe <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-28-2013
On Friday 28 June 2013 11:51, in comp.lang.c, (E-Mail Removed) wrote:

> Giuseppe <(E-Mail Removed)> 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) (E-Mail Removed)
> <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"
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-28-2013
On Friday 28 June 2013 11:55, in comp.lang.c, (E-Mail Removed)
wrote:

> On Friday 28 June 2013 11:51, in comp.lang.c, (E-Mail Removed) 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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-28-2013
Lew Pitcher <(E-Mail Removed)> writes:
> On Friday 28 June 2013 11:51, in comp.lang.c, (E-Mail Removed) 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Unexpected truncation behavior glen herrmannsfeldt C Programming 23 06-30-2013 05:18 AM
Re: Unexpected truncation behavior mark.bluemel@gmail.com C Programming 3 06-29-2013 12:03 AM
MySql Data Truncation Marcelo Java 3 12-21-2005 01:29 AM
Text Truncation manas ASP .Net 1 07-01-2005 10:41 PM
Truncation of long filename plxjb Computer Information 0 09-28-2004 06:40 AM



Advertisments