hypot
hypot(float $x, float $y): float
Calculates the hypotenuse of a right-angle triangle (distance from (x, y) to the origin). Mathematically equal to sqrt(x^2+y^2), but it scales before squaring so intermediate values never overflow — the extreme 3e200/4e200 contrast is the heart of this entry: the naive formula squares its way to INF while hypot still returns 5e200.
C C23
#include <math.h>
#include <stdio.h>
int main(void) {
// hypot(3): the overflow-safe way to compute sqrt(x*x + y*y)
printf("%.10f\n", hypot(3.0, 4.0));
// the naive formula overflows: 3e200 * 3e200 is far beyond double max (~1.8e308)
double naive = sqrt(3e200 * 3e200 + 4e200 * 4e200);
printf("%d\n", isinf(naive) != 0 ? 1 : 0);
// hypot scales internally, so the same extreme pair stays finite
printf("%.10f\n", hypot(3e200, 4e200) / 1e200);
printf("%d\n", isinf(hypot(3e200, 4e200)) != 0 ? 1 : 0);
return 0;
}
5.0000000000 1 5.0000000000 0
C++ C++26
#include <cmath>
#include <cstdio>
int main() {
// std::hypot: the overflow-safe way to compute sqrt(x*x + y*y)
std::printf("%.10f\n", std::hypot(3.0, 4.0));
// the naive formula overflows: 3e200 * 3e200 is far beyond double max (~1.8e308)
double naive = std::sqrt(3e200 * 3e200 + 4e200 * 4e200);
std::printf("%d\n", std::isinf(naive) ? 1 : 0);
// hypot scales internally, so the same extreme pair stays finite
std::printf("%.10f\n", std::hypot(3e200, 4e200) / 1e200);
std::printf("%d\n", std::isinf(std::hypot(3e200, 4e200)) ? 1 : 0);
return 0;
}
5.0000000000 1 5.0000000000 0
C# v11.0.100
// .NET has no Math.Hypot: the naive formula overflows for large inputs,
// so the robust version scales down by the larger side before squaring
static double Hypot(double x, double y) {
x = Math.Abs(x);
y = Math.Abs(y);
double m = Math.Max(x, y);
if (m == 0.0) return 0.0;
double r = Math.Min(x, y) / m;
return m * Math.Sqrt(1.0 + r * r);
}
double naive = Math.Sqrt(3e200 * 3e200 + 4e200 * 4e200);
Console.WriteLine(Hypot(3, 4).ToString("F10"));
Console.WriteLine(double.IsInfinity(naive) ? 1 : 0);
Console.WriteLine((Hypot(3e200, 4e200) / 1e200).ToString("F10"));
Console.WriteLine(double.IsInfinity(Hypot(3e200, 4e200)) ? 1 : 0);
5.0000000000 1 5.0000000000 0
Go v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
// math.Hypot: the overflow-safe way to compute sqrt(x*x + y*y)
fmt.Printf("%.10f\n", math.Hypot(3, 4))
// the naive formula overflows: 3e200 * 3e200 is far beyond float64 max (~1.8e308)
// (values in variables so the overflow happens at run time, not compile time)
x, y := 3e200, 4e200
naive := math.Sqrt(x*x + y*y)
if math.IsInf(naive, 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
// hypot scales internally, so the same extreme pair stays finite
fmt.Printf("%.10f\n", math.Hypot(x, y)/1e200)
if math.IsInf(math.Hypot(x, y), 0) {
fmt.Println(1)
} else {
fmt.Println(0)
}
}
5.0000000000 1 5.0000000000 0
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Math.hypot: the overflow-safe way to compute sqrt(x*x + y*y)
System.out.printf("%.10f%n", Math.hypot(3, 4));
// the naive formula overflows: 3e200 * 3e200 is far beyond double max (~1.8e308)
double naive = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
System.out.println(Double.isInfinite(naive) ? 1 : 0);
// hypot scales internally, so the same extreme pair stays finite
System.out.printf("%.10f%n", Math.hypot(3e200, 4e200) / 1e200);
System.out.println(Double.isInfinite(Math.hypot(3e200, 4e200)) ? 1 : 0);
}
}
5.0000000000 1 5.0000000000 0
JavaScript v26.13.1
// Math.hypot: the overflow-safe way to compute sqrt(x*x + y*y)
console.log(Math.hypot(3, 4).toFixed(10));
// the naive formula overflows: 3e200 * 3e200 is far beyond double max (~1.8e308)
const naive = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
console.log(Number.isFinite(naive) ? 0 : 1);
// hypot scales internally, so the same extreme pair stays finite
console.log((Math.hypot(3e200, 4e200) / 1e200).toFixed(10));
console.log(Number.isFinite(Math.hypot(3e200, 4e200)) ? 0 : 1);
5.0000000000 1 5.0000000000 0
PHP v8.6.0
// hypot(): the overflow-safe way to compute sqrt(x*x + y*y)
printf("%.10f\n", hypot(3, 4));
// the naive formula overflows: 3e200 * 3e200 is far beyond float max (~1.8e308)
$naive = sqrt(3e200 * 3e200 + 4e200 * 4e200);
printf("%d\n", is_infinite($naive) ? 1 : 0);
// hypot scales internally, so the same extreme pair stays finite
printf("%.10f\n", hypot(3e200, 4e200) / 1e200);
printf("%d\n", is_infinite(hypot(3e200, 4e200)) ? 1 : 0);
5.0000000000 1 5.0000000000 0
Python v3.15.2
import math
# math.hypot: the overflow-safe way to compute sqrt(x*x + y*y)
print(f"{math.hypot(3, 4):.10f}")
# the naive formula overflows: 3e200 * 3e200 is far beyond float max (~1.8e308)
naive = math.sqrt(3e200 * 3e200 + 4e200 * 4e200)
print(1 if math.isinf(naive) else 0)
# hypot scales internally, so the same extreme pair stays finite
print(f"{math.hypot(3e200, 4e200) / 1e200:.10f}")
print(1 if math.isinf(math.hypot(3e200, 4e200)) else 0)
5.0000000000 1 5.0000000000 0
Rust v1.99.1
fn main() {
// f64::hypot: the overflow-safe way to compute sqrt(x*x + y*y)
println!("{:.10}", 3.0_f64.hypot(4.0));
// the naive formula overflows: 3e200 * 3e200 is far beyond f64::MAX (~1.8e308)
let naive = (3e200_f64 * 3e200_f64 + 4e200_f64 * 4e200_f64).sqrt();
println!("{}", if naive.is_infinite() { 1 } else { 0 });
// hypot scales internally, so the same extreme pair stays finite
let big = 3e200_f64.hypot(4e200);
println!("{:.10}", big / 1e200);
println!("{}", if big.is_infinite() { 1 } else { 0 });
}
5.0000000000 1 5.0000000000 0
TypeScript v26.13.1
// Math.hypot: the overflow-safe way to compute sqrt(x*x + y*y)
console.log(Math.hypot(3, 4).toFixed(10));
// the naive formula overflows: 3e200 * 3e200 is far beyond double max (~1.8e308)
const naive: number = Math.sqrt(3e200 * 3e200 + 4e200 * 4e200);
console.log(Number.isFinite(naive) ? 0 : 1);
// hypot scales internally, so the same extreme pair stays finite
console.log((Math.hypot(3e200, 4e200) / 1e200).toFixed(10));
console.log(Number.isFinite(Math.hypot(3e200, 4e200)) ? 0 : 1);
5.0000000000 1 5.0000000000 0
Language differences
PHP: hypot($x, $y) is built in; mathematically equal to sqrt(x^2+y^2), but it scales by the larger side before squaring, so intermediates never overflow — the soul of this entry is the extreme contrast: the naive formula squares its way to INF at 3e200/4e200, while hypot still returns exactly 5e200 (normalized by dividing 1e200 to print 5.0000000000).
C/C++: hypot(3) (a POSIX function, not ISO C; <math.h> included explicitly); test INF with isinf(x) != 0 — the macro returns a nonzero int, never write == 1.
Java: Math.hypot; C#: .NET has no built-in hypot — Math.Sqrt(x*x+y*y) overflows to Infinity at 3e200/4e200 (the 1 on output line 2), and the example hand-writes the robust scaled version as the fix.
Python: math.hypot; JavaScript/TypeScript: Math.hypot; Go: math.Hypot; Rust: f64::hypot — C# is the only one of the ten that needs a hand-written version.
Precision policy: extreme values are always normalized by 1e200 and printed with %.10f (10 decimals) — cross-platform/cross-compiler ULP tails (around 1e-16 relative) cannot touch the first 10 digits; the INF checks derive booleans from the isinf family, so the 1/0 output is environment-independent.
The ordinary 3/4/5 case: hypot(3, 4) is exactly 5.0 in every language (representable in double precision), so all ten outputs agree under %.10f.
Go detail: 3e200*3e200 as a constant expression fails at compile time with "constant overflows float64"; the example puts the values in variables so the overflow happens at run time.
See also: sqrt has its own entry; the INF/NaN predicates are covered by the is_infinite / is_nan entries.