Note that there are some explanatory texts on larger screens.

plurals
  1. USSilverstorm
    primarykey
    data
    text
    plurals
    1. CO@EricPostpischil Strangely I cannot reproduce the issue with (0.9f,1) without cast, so I cannot insist until further test. However If you write directly the value you should verify that the passed value is seen as float and not as double. Are you sure that if you write (0.9,1) is seen as float and not as double? In more situations this doesn't even compile trowing an error about the argument. But what is the point? If you pass float and assign the result to a float the method do what is intended to do, either if you pass double and convert the method to double as said. So what is the problem?
      singulars
    2. CO@EricPostpischil Effectively as have you said System.out.format unlike System.out.println shows extended value output, but this doesn't matter, the important is that the approximation doesn't change the values wrongly, out of the needed decimals precision, what is asked for is a method that APPROXIMATES the number of decimal digits to avoid to output a huge number of decimals and this is what the method does. The differences between printf and println shows that there is no precision also in the java base output. If you need a high precision you don't use approximation and you don't use float.
      singulars
    3. COIf you write float x=decimalDigits(1, 0.8999999761581421); System.out.println("Value is: "+x); Print "Value is 0.9" OR float x=decimalDigits(1, (float)0.9f); System.out.println("Value is: "+x); Print "Value is 0.9" That is the same code written in different way. But if you write without any cast double x=decimalDigits(1, 0.9f); System.out.println("Value is: "+x); Print "Value is 0.89999997615814208984375" So the cast is necessary, if you change all parameters of the method from float to double this not happen.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload