sqrt / pi
sqrt(float $num): float · pi(): float
Square root and pi: sqrt returns the float square root (negative input yields a silently propagating NAN, not an error), and pi() holds the same double-precision pi as the M_PI constant; under IEEE 754 every language's output matches exactly.
C C23
#include <math.h>
#include <stdio.h>
/* sqrt from libm; M_PI is the POSIX/compiler-extension macro (ISO C has no pi constant) */
int main(void) {
printf("%d\n", sqrt(16.0) == 4.0);
printf("%.15f\n", sqrt(2.0));
printf("negative: %s\n", isnan(sqrt(-1.0)) ? "NaN returned" : "error");
printf("%.15f\n", M_PI);
printf("%.15f\n", M_SQRT2); /* extension constant for sqrt(2) */
return 0;
}
1 1.414213562373095 negative: NaN returned 3.141592653589793 1.414213562373095
C++ C++26
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>
// C++20: std::numbers::pi is the STANDARD constant (C's M_PI is only an extension)
int main() {
std::cout << std::boolalpha << (std::sqrt(16.0) == 4.0) << "\n";
std::cout << std::fixed << std::setprecision(15) << std::sqrt(2.0) << "\n";
std::cout << "negative: " << (std::isnan(std::sqrt(-1.0)) ? "NaN returned" : "error") << "\n";
std::cout << std::numbers::pi << "\n";
std::cout << std::numbers::sqrt2 << "\n";
return 0;
}
true 1.414213562373095 negative: NaN returned 3.141592653589793 1.414213562373095
C# v11.0.100
using System;
using System.Globalization;
// Math.Sqrt/Math.PI: fully native, IEEE 754 doubles agree across languages
Console.WriteLine(Math.Sqrt(16.0) == 4.0);
Console.WriteLine(Math.Sqrt(2.0).ToString("F15", CultureInfo.InvariantCulture));
Console.WriteLine("negative: " + (double.IsNaN(Math.Sqrt(-1.0)) ? "NaN returned" : "error"));
Console.WriteLine(Math.PI.ToString("F15", CultureInfo.InvariantCulture));
True 1.414213562373095 negative: NaN returned 3.141592653589793
Go v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Sqrt(16.0) == 4.0)
fmt.Printf("%.15f\n", math.Sqrt(2))
fmt.Println("negative:", math.IsNaN(math.Sqrt(-1))) // NaN returned, like PHP
fmt.Println(float64(math.Pi) == 3.141592653589793) // untyped-constant compare is arbitrary-precision: cast to double first
fmt.Printf("%.15f\n", math.Pi)
fmt.Printf("%.15f\n", math.Sqrt2) // built-in sqrt(2) constant
}
true 1.414213562373095 negative: true true 3.141592653589793 1.414213562373095
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Math.sqrt/Math.PI: fully native, IEEE 754 doubles agree across languages
System.out.println(Math.sqrt(16.0) == 4.0);
System.out.println(String.format("%.15f", Math.sqrt(2)));
System.out.println("negative: " + (Double.isNaN(Math.sqrt(-1.0)) ? "NaN returned" : "error"));
System.out.println(String.format("%.15f", Math.PI));
}
}
true 1.414213562373095 negative: NaN returned 3.141592653589793
JavaScript v26.13.1
console.log(Math.sqrt(16) === 4);
console.log(Math.sqrt(2).toFixed(15)); // full double precision
console.log("negative:", Number.isNaN(Math.sqrt(-1)) ? "NaN returned" : "error");
console.log(Math.PI.toFixed(15));
true 1.414213562373095 negative: NaN returned 3.141592653589793
PHP v8.6.0
// sqrt: float square root; negative input yields NAN (silent, not an error)
var_dump(sqrt(16.0) === 4.0);
echo sprintf("%.15f", sqrt(2)), "\n"; // full double precision
echo "negative: ", is_nan(sqrt(-1)) ? "NaN returned" : "error", "\n";
var_dump(pi() === M_PI); // pi() and the M_PI constant hold the identical double
echo sprintf("%.15f", pi()), "\n";
bool(true) 1.414213562373095 negative: NaN returned bool(true) 3.141592653589793
Python v3.15.2
import math
print(math.sqrt(16.0) == 4.0)
print(f"{math.sqrt(2):.15f}")
try:
math.sqrt(-1.0)
print("negative: no error")
except ValueError:
print("negative: ValueError") # Python raises; PHP returns NAN silently
print(math.pi == 3.141592653589793)
print(f"{math.pi:.15f}")
True 1.414213562373095 negative: ValueError True 3.141592653589793
Rust v1.99.1
use std::f64::consts::{PI, SQRT_2};
fn main() {
println!("{}", 16f64.sqrt() == 4.0);
println!("{:.15}", 2f64.sqrt());
println!("negative: {}", if (-1f64).sqrt().is_nan() { "NaN returned" } else { "error" });
println!("{:.15}", PI);
println!("{:.15}", SQRT_2); // built-in sqrt(2) constant
}
true 1.414213562373095 negative: NaN returned 3.141592653589793 1.414213562373095
TypeScript v26.13.1
console.log(Math.sqrt(16) === 4);
console.log(Math.sqrt(2).toFixed(15)); // full double precision
console.log("negative:", Number.isNaN(Math.sqrt(-1)) ? "NaN returned" : "error");
console.log(Math.PI.toFixed(15));
true 1.414213562373095 negative: NaN returned 3.141592653589793
Language differences
A gap-filler entry for the long-missing sqrt: square roots and pi are the two most basic faces of float math. Negative input splits into two camps — PHP/JS/Rust/Go/C/C++ return NaN and let it propagate silently (detect via is_nan/IsNaN), while Python's math.sqrt raises ValueError (explicit error beats silent NaN — both philosophies have adherents; always check when porting across languages); C# returns NaN too, detected via double.IsNaN.
The pi constant landscape: PHP's pi() function and M_PI constant are identical (this entry asserts ===); M_PI does not exist in ISO C (it is a POSIX/compiler-extension macro), and only C++20 standardized std::numbers::pi; math.pi (Python), Math.PI (Java/JS/C#), math.Pi (Go) and std::f64::consts::PI (Rust) are all standard built-ins; Go/Rust/C++ also carry built-in sqrt(2) constants (math.Sqrt2/SQRT_2/std::numbers::sqrt2) while PHP must compute sqrt(2) itself.
Deterministic assertions: sqrt(16.0)===4.0 (exact), sqrt(2)'s %.15f full-double expansion 1.414213562373095, and pi's 3.141592653589793 (the nearest double) — IEEE 754 doubles agree across languages, so the outputs cross-verify.
Related existing entries: ceil/floor (the rounding family) and the bcmath series (arbitrary precision — arbitrary-digit pi needs BC math or a dedicated library; floats carry only ~15-17 significant digits).