TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: NEIL HELLER
from: GEORGE WHITE
date: 1998-02-23 13:56:00
subject: What`S Happening Here?

Hi Neil,
Yoyu asked:
NH>Yesterday I wrote a short program which caused me to have even more
NH>questions after I finished than I did when I started.
NH>int
NH>main(void)
NH>{
NH>    long    longint1 = 11L,
NH>            longint2 =  3L,
NH>            result1,
NH>            result2;
NH>    float   multiplier = 1.12345F;
NH>    result1 = longint1 * longint2 * multiplier;   /* 37 */
Fine, the float result is 37.07384 before the cast back to long.
NH>    result2 = longint1 * longint2 * multiplier + 0.9999F;   /* 38 */
Again fine, as the float result is 38.07375 before the cast back to
long.
NH>    return(0);
NH>}
NH>Can someone explain to me just which variables were implicitly cast by
NH>the compiler (I used MSVC 5.0)?
According to the rules the two longs should be promoted to float before
the calculation, the calculation made and then the result cast back to
long (which discards any fractional part).
NH>                                Would result2 have been different had
NH>I placed a left paren following the '=' and a right paren before the
NH>'+'?  Was it necessary to place 'F' in each of the two places used
NH>(after the constants to which they referred)?
No, Yes.
George
 * SLMR 2.1a * All Trademarks acknowledged (just in case ).
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.