problem with maths

float a=1/10;
print (a);                  // 0.0
float b=1*0.1;
print (b);                  //0.1
Tagged:

Answers

  • thank you so much !!!

  • edited January 2014
    final float a = 1/1e1;
    println(a);
    
    final float b = 1/10.0;
    println(b);
    
    final float c = (float) 1/10;
    println(c);
    
    exit();
    
  • The reason for 1 being displayed as the result is that compiler treats the division result to be an integer. The numerator and denominator in the expression are both integers hence the result. This can be avoided by explicitly type casting the data as float type.

  • edited February 2014

    @GoToLoop - not sure why you have made these constants :-/ but anyway here are three more for your collection.

    float d = 1/10f;
    println(d);
    
    float e = 1f/10;
    println(e);
    
    float f = 1/(float)10;
    println(f);
    
Sign In or Register to comment.