Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about mixed scalar type operation

Reply
Thread Tools

Question about mixed scalar type operation

 
 
linq936@hotmail.com
Guest
Posts: n/a
 
      05-14-2007
Hi,
I have this little test program:

#include <stdio.h>

int main(){
float f1 = 2.2;
double d1 = 3.3;
int j = f1 * d1;
int i = (int) f1 * d1;
int i2 = (int) (f1 * d1);
printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
}

I use gcc on Linux to compile it, there is no warning and the result
is like this:

2066953011, i=1075644989, j=6, i2=7

I have several questions:

1. Why there is no warning in compiler when I assign the
multiplication of float and double to integer?

2. As for the result, I can understand how i2 comes from, but others
do not make sense to me.

Could you give me some pointer?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-14-2007
In article <. com>,
<> wrote:

> I have this little test program:


>#include <stdio.h>


>int main(){
> float f1 = 2.2;
> double d1 = 3.3;
> int j = f1 * d1;
> int i = (int) f1 * d1;
> int i2 = (int) (f1 * d1);
> printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);
>}


> I use gcc on Linux to compile it, there is no warning and the result
>is like this:


>2066953011, i=1075644989, j=6, i2=7


> I have several questions:


>1. Why there is no warning in compiler when I assign the
>multiplication of float and double to integer?


It is not considered a syntactic or semantic error to do such
a multiplication and assignment -- the results are well defined.
The compiler is not required to return a warning for well defined
behaviour that -might- have been the result of the programmer having
overlooked something (or might have been perfectly intended.)


>2. As for the result, I can understand how i2 comes from, but others
>do not make sense to me.


In your printf() statement, f1*d1 is going to be a double, and you
attempt to print that double out using an integer format. If it
happens that on your machine that a double is "wider" than an
integer, the %d format will only use up some of the bytes of the double,
possibly leaving the other bytes from the double in a location to
be printed by the next format element along, i=%d . Once that has
happened, everything else gets shifted along from what you expect.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-14-2007
wrote:
> Hi,
> I have this little test program:


> #include <stdio.h>


> int main(){
> float f1 = 2.2;
> double d1 = 3.3;
> int j = f1 * d1;

This is exactly the same as the i2 case below

> int i = (int) f1 * d1;

This is (int) 2.2 * 3.3 ->
2 * 3.3 ->
6.6
assigned to an int as 6

> int i2 = (int) (f1 * d1);

This is (int) (2.2 * 3.3) =>
(int) (7.26) ->
7

> printf("%d, i=%d, j=%d, i2=%d\n", f1*d1, i, j, i2);

^^
This is an error. f1*d1 is a floating point value.
%d is a specifier for signed ints.
Unless a double and an int have exactly the same size,
you have hopelessly munged the arguments to printf.

Unless you are using a C99 compiler, you need to return a
value here, e.g.
return 0;

> }


> I use gcc on Linux to compile it, there is no warning and the result
> is like this:


> 2066953011, i=1075644989, j=6, i2=7


After you change the erroneous "%d" to "%g", the output should look like:
7.26, i=6, j=7, i2=7



> I have several questions:


> 1. Why there is no warning in compiler when I assign the
> multiplication of float and double to integer?


Because there is absolutely nothing wrong with doing so, and there is
nothing to warn about.


> 2. As for the result, I can understand how i2 comes from, but others
> do not make sense to me.


No, you can't understand "how i2 comes from", because the erroneous "%d"
makes a complete hash of your output line.

> Could you give me some pointer?


Don't feed us straight lines.


 
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
Question about mixed scalar type operation linq936@hotmail.com C Programming 2 07-01-2007 01:00 AM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
How do I restrict the type of a text node in a mixed, complex-type element? Chishun Kwong XML 0 03-03-2005 05:09 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57