Results for “Numbers & calculations”
Small, practical examples. Search by task or narrow things down below.
Check an upper integer bound before adding
C++: For these nonnegative values, compare against the maximum minus the addend first.
C++✓ Tested
Check whether a floating-point value is finite
Check whether a floating-point value is finite in a complete Rust program using the standard library.
Rust✓ Tested
Check whether a value is a safe integer
JavaScript: Number.isSafeInteger also checks the exact-integer range supported by Number.
JavaScript✓ Tested
Check whether a value is a safe integer
TypeScript: Number.isSafeInteger also checks the exact-integer range supported by Number.
TypeScript✓ Tested
Clamp a number between inclusive bounds
JavaScript: Combine Math.min and Math.max when lower is no greater than upper.
JavaScript✓ Tested
Clamp a number between inclusive bounds
TypeScript: Combine Math.min and Math.max when lower is no greater than upper.
TypeScript✓ Tested
Clamp a number to a valid interval
Clamp a number to a valid interval in a complete Rust program using the standard library.
Rust✓ Tested
Clamp a number to inclusive bounds
C#: Math.Clamp returns the nearest endpoint when the value lies outside the range.
C#✓ Tested
Compare bounded floating-point results with a tolerance
JavaScript: Use an absolute error threshold for values known to be near one.
JavaScript✓ Tested
Compare bounded floating-point results with a tolerance
TypeScript: Use an absolute error threshold for values known to be near one.
TypeScript✓ Tested
Compare floating-point results with a tolerance
Java: For this bounded example, compare the absolute difference against a chosen error threshold.
Java✓ Tested
Compute an integer weighted average
SQL: Multiply before summing and guard a zero total weight with NULLIF.
SQL✓ Tested