Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
class WideSample {
  public static void main(String[] args) {
    int big = 1234567890;
                
    float approx = big;
    if(Integer.highestOneBit(big) <= Math.pow(2, 23)) { //the significand can store at most 23 bits
      System.out.println(big - (int)approx);  //always prints zero now
    }
    else {
      //handle error
      throw  //precision errornew ArithmeticException("Insufficient precision");
    }
  }
}

Risk Assessment

Casting numeric types to wider floating-point types may lose information.

...