# Assigning fractions to primitive integral/decimal types?

s khosa

Ranch Hand

Posts: 72

Dave Johnson

Ranch Hand

Posts: 111

posted 12 years ago

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.

floating point operations

Hope they help, Dave.

Marlene Miller

Ranch Hand

Posts: 1392

posted 12 years ago

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 ]

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 ]