aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning fractions to primitive integral/decimal types? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning fractions to primitive integral/decimal types?" Watch "Assigning fractions to primitive integral/decimal types?" New topic
Author

Assigning fractions to primitive integral/decimal types?

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
How does compiler evaluate assignments of fractional numbers to integral types and decimal types?
e.g. int i =1/3;
float f = 4/8;
???
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Try the search facility on this forum & type fraction as your search. I found quite a few when I did it. floating point math
floating point operations
Hope they help, Dave.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
When one number is divided by another number,
First binary numeric promotion occurs:
If one number is a double, the other number is promoted to a double.
Otherwise, if one number is a float, the other number is promoted to a float.
Otherwise, if one number is a long, the other number is promoted to a long.
Otherwise, both numbers are promoted to type int.
Now, both numbers are of the same type.
If both numbers are integral (type int or type long), ordinary math division occurs and the result is rounded toward 0 to the nearest integer. Sometimes this is called truncation.
If both numbers are floating point (type float or type double), ordinary math division occurs and the result is rounded to the nearest number of type float or type double.
There are a lot of special cases for floating point arithmetic. They are defined in the standard IEEE 754-1985.
For example, 1/3
Both numbers are of type int. The result is 0.333333333 etc. The result is rounded to 0.
For example, 1.0f/3.0f
Both numbers are of type float. The result is 0.333333333 etc. The result is rounded to 0.33333334, because it is closer than 0.33333330. The numbers 0.33333331, 0.33333332 and 0.33333333 do not have a floating point representation.
[ June 01, 2003: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning fractions to primitive integral/decimal types?