Java Math clamp() Method

The Math.clamp() method in Java is used to constrain a value to fit within a specified range defined by a minimum and maximum value. 

Table of Contents

  1. Introduction
  2. clamp() Method Syntax
  3. Overloaded clamp() Methods
  4. Examples
    • clamp(double value, double min, double max)
    • clamp(float value, float min, float max)
    • clamp(long value, int min, int max)
    • clamp(long value, long min, long max)
  5. Real-World Use Case
  6. Conclusion

Introduction

The Math.clamp() method ensures that a given value falls within a specified range. If the value is less than the minimum, the method returns the minimum. If the value is greater than the maximum, the method returns the maximum. Otherwise, it returns the value itself.

clamp() Method Syntax

The syntax for the clamp() method varies depending on the types of the arguments:

clamp(double value, double min, double max)

public static double clamp(double value, double min, double max)

clamp(float value, float min, float max)

public static float clamp(float value, float min, float max)

clamp(long value, int min, int max)

public static int clamp(long value, int min, int max)

clamp(long value, long min, long max)

public static long clamp(long value, long min, long max)

Parameters:

  • value: The value to be clamped.
  • min: The minimum bound of the range.
  • max: The maximum bound of the range.

Returns:

  • The clamped value that fits within the specified range.

Overloaded clamp() Methods

The Math.clamp() method is overloaded to handle different combinations of primitive data types: double, float, int, and long. Each version returns the clamped value within the specified range.

Examples

clamp(double value, double min, double max)

The clamp(double value, double min, double max) method returns the clamped value for a double type.

Example

public class ClampDoubleExample {
    public static void main(String[] args) {
        double value = 10.5, min = 0.0, max = 10.0;

        double result = Math.clamp(value, min, max);

        System.out.println("Clamped value of " + value + " between " + min + " and " + max + " is " + result);
    }
}

Output:

Clamped value of 10.5 between 0.0 and 10.0 is 10.0

clamp(float value, float min, float max)

The clamp(float value, float min, float max) method returns the clamped value for a float type.

Example

public class ClampFloatExample {
    public static void main(String[] args) {
        float value = -5.5f, min = 0.0f, max = 10.0f;

        float result = Math.clamp(value, min, max);

        System.out.println("Clamped value of " + value + " between " + min + " and " + max + " is " + result);
    }
}

Output:

Clamped value of -5.5 between 0.0 and 10.0 is 0.0

clamp(long value, int min, int max)

The clamp(long value, int min, int max) method returns the clamped value for a long type.

Example

public class ClampLongIntExample {
    public static void main(String[] args) {
        long value = 15L;
        int min = 0, max = 10;

        int result = Math.clamp(value, min, max);

        System.out.println("Clamped value of " + value + " between " + min + " and " + max + " is " + result);
    }
}

Output:

Clamped value of 15 between 0 and 10 is 10

clamp(long value, long min, long max)

The clamp(long value, long min, long max) method returns the clamped value for a long type.

Example

public class ClampLongLongExample {
    public static void main(String[] args) {
        long value = -20L, min = -10L, max = 10L;

        long result = Math.clamp(value, min, max);

        System.out.println("Clamped value of " + value + " between " + min + " and " + max + " is " + result);
    }
}

Output:

Clamped value of -20 between -10 and 10 is -10

Real-World Use Case

Limiting Sensor Values

In real-world scenarios, the Math.clamp() method can be used to limit sensor values to a specified range. For example, if a temperature sensor should only report values between -40°C and 50°C, the clamp() method can ensure that any reported value outside this range is adjusted to fit within the specified bounds.

Example

public class SensorValueClampExample {
    public static void main(String[] args) {
        double sensorValue = 55.0;
        double minTemperature = -40.0;
        double maxTemperature = 50.0;

        double clampedValue = Math.clamp(sensorValue, minTemperature, maxTemperature);

        System.out.println("Clamped sensor value: " + clampedValue);
    }
}

Output:

Clamped sensor value: 50.0

Conclusion

The Math.clamp() method in Java provides a way to constrain a given value within a specified range. By understanding how to use this method and its overloaded versions, you can handle various scenarios where values need to be limited to a specific range. 

Whether you are working with doubles, floats, integers, or long integers, the clamp() method offers a reliable tool for ensuring values fit within defined bounds.

Comments