Library contents

Java Math.getExponent

Extract the unbiased base-two exponent of a normal floating-point value.

A simple example

Math.getExponent

  1. Start with 8.0
  2. Do this Read the binary exponent
  3. Result 3
Eight is 1 times 2 to the third power.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.getExponent(8.0));
    }
}
Output
3

Syntax

Java syntax
Math.getExponent(reading)
JDK 21 method signatures (2)
public static int getExponent(float f)
public static int getExponent(double d)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Find the binary scale of a sensor reading

A sensor stores readings in a binary floating-point format with limited precision. Inspect or adjust the scale of the sample value so the calibration code can account for representable steps.

From the programdouble reading = 12.0
Result3
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        double reading = 12.0;
        System.out.println(Math.getExponent(reading));
    }
}
Output
3

Twelve lies between two cubed and two to the fourth, so its exponent is three.

Advertisements

Choose a range on a sound meter

A normalized meter reading is 20. The instrument labels its binary range with the exponent of the power of two at the bottom of the interval.

Given20
Worked result4

Using 20 gives 4 for this measurement.

Classify a dim light reading

A light sensor returns 0.75 in its normalized units. The graph groups positive samples by their unbiased binary exponent.

Given0.75
Worked result-1

Using 0.75 gives -1 for this measurement.

What to watch for

Zero, subnormal readings, infinities, and NaN have special exponent results.

Documentation for Math.getExponent