0
votes

Is there a magnitude (decimal range) on which the conversion from double to float is based ? At the beginning I was thinking so but the exponent part confused me .. So what rules are governing that conversion

second, When converting from floating point to integer is the rule just taking out the fraction part ? meaning 12.5 * 10^-6 will evaluate to zero ?

EDIT:

I will Write my question in another more precise form :

  • What are the binary-level rules to transform double to float in java (ie sign,mantissa,exponent manipulation during the narrowing conversion)

  • Are those binary rules correspond to decimal rules (human readable) so that the result could be predictable based on the source (double) value ?

  • Is there any Similar rules when converting either floating type rules to integer types ?

Thanks

1
I'm not sure what you mean by 'governing that conversion' and 'rules governing narrowing conversion'. That combination of words just doesn't make sense to me. Are you asking how Java converts from double to float and from float to int? - Nathan Loyer
yes, for example if we are talking about floating point to integer the fraction part will be removed and the integer part will be either converted to long or to int and whether stay at int or most significant bits truncated to leave a number not necessary the original , BUT from double to float how that is done on the binary level (ieee 754 mantissa exponent sign) may illustrate the idea for me and remove confusion - AbdAllah Talaat
double to float conversion on binary level is rather simple: keep the sign, cast the signed int 11 used as exponent to a signed int 8 and discard the last 29 bit of the mantissa (rounding if necessary). As a result you have converted the 1+11+52 bit in a double to 1+8+23 bit in a float. - Poohl
Wait @Poohl .... you have raised a light in my mind ... I could now reform my question I wasn't able to write precisely at the beginning ... is the result predictable ? could I predict what value will result based on the original value ?... or that binary manipulation doesn't correspond to decimal human readable rule ? .. I will edit the original post - AbdAllah Talaat
@Poohl Yes, but is that what Java does...? I don't know how Java does either conversion. Was just hoping to clarify the question being asked. @AbdAllah You could try looking into the code for Float.valueOf(double) (I'm guessing at the name of it, it should be something like that) - Nathan Loyer

1 Answers

0
votes

Ok, since according to the comment the binary workings are requested:

In general see https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.2.4

A) double to float:

The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision.

So the double is rounded to the next float. The conversion can be described as follows:

  • Keep the sign.
  • cast the signed int 11 used as exponent to a signed int 8. If this is not possible, because it causes an overflow, return Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY.
  • and discard the last 29 bit of the mantissa by rounding.

As a result you have converted the 1+11+52 bit in a double to 1+8+23 bit in a float.

B) floating point to int:

The Java programming language uses round toward zero when converting a floating value to an integer (ยง5.1.3), which acts, in this case, as though the number were truncated, discarding the mantissa bits.

So in simple terms: Write down the number (not in scientific notation) and remove everything beyond the .

In the PC it probably works by shifting the mantissa exponent bits in the appropriate direction, then discarding (not rounding!) any remaining decimal fractions. And in the end multiplying the result by -1 if the float was negative. in short:

 int = float.mantissa << float.exponent * (float < 0 ? -1 : 1)

Can all of this be done by hand and thus results predicted: yes, of course, otherwise these definitions would be useless. But it requires you work with the binary-representation of the numbers you're dealing with, as e.g. "discarding digits" creates different results depended on the base.