fmod

fmod(float $num1, float $num2): float

Float modulo whose result keeps the sign of the dividend.

C C23

C23
#include <math.h>
#include <stdio.h>
int main(void) {
    printf("%g\n", fmod(5.7, 1.3)); // result keeps the sign of the dividend
    printf("%g\n", fmod(-5.7, 1.3));
    return 0;
}
Output
0.5
-0.5

C++ C++26

C++26
#include <cmath>
#include <cstdio>
int main() {
    std::printf("%g\n", std::fmod(5.7, 1.3)); // sign follows the dividend (IEEE truncated remainder)
    std::printf("%g\n", std::fmod(-5.7, 1.3));
    return 0;
}
Output
0.5
-0.5

C# v11.0.100

v11.0.100
Console.WriteLine(5.7 % 1.3); // % on doubles matches fmod semantics
Console.WriteLine(-5.7 % 1.3);
Output
0.5
-0.5

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.Mod(5.7, 1.3))
    fmt.Println(math.Mod(-5.7, 1.3))
}
Output
0.5
-0.5

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.println(5.7 % 1.3);
        System.out.println(-5.7 % 1.3);
    }
}
Output
0.5
-0.5

JavaScript v26.13.1

v26.13.1
console.log(5.7 % 1.3);
console.log(-5.7 % 1.3);
Output
0.5
-0.5

PHP v8.6.0

v8.6.0
echo fmod(5.7, 1.3), "\n";
echo fmod(-5.7, 1.3);
Output
0.5
-0.5

Python v3.15.2

v3.15.2
import math

print(math.fmod(5.7, 1.3))
print(math.fmod(-5.7, 1.3))
Output
0.5
-0.5

Rust v1.99.1

v1.99.1
fn main() {
    println!("{}", 5.7f64 % 1.3); // % on f64 is the fmod-style truncated remainder
    println!("{}", -5.7f64 % 1.3);
}
Output
0.5
-0.5

TypeScript v26.13.1

v26.13.1
console.log(5.7 % 1.3);
console.log(-5.7 % 1.3);
Output
0.5
-0.5

Language differences

C: fmod is IEEE 754's truncated remainder — the result keeps the dividend's sign (fmod(-5.7, 1.3) == -0.5); C's % is integer-only, so float modulo must go through the library function (example); edge behavior: fmod(x, 0) is NaN, fmod(x, INF) is x itself.

C++: std::fmod matches the C semantics (example); std::remainder is a third semantics (least-magnitude remainder, results can land at -divisor/2) — don't confuse it with fmod.

C#: the floating % already means exactly this (example); also note Math.IEEERemainder is a third semantics (least-magnitude remainder, sign may follow the divisor) — don't confuse it with %.

Go: math.Mod (example); math.Remainder offers the least-magnitude semantics; Go's integer % and float Mod mind their own domains with no cross-truncation.

Java: the floating % operator already means exactly this, no dedicated function needed; Math.floorMod serves only the integer floor semantics.

JavaScript: the floating % operator already means exactly this, no dedicated function needed; % never truncates its operands — one story for ints and floats alike.

PHP: the biggest difference — the % operator truncates operands to int (5.7 % 1.3 equals 5 % 1 = 0), so float modulo must use fmod (example); edges: fmod(x, 0) is NAN, fmod(x, INF) is x; for the integer-side story see the intdiv entry.

Python: % is floor semantics (-5.7 % 1.3 == 0.8, sign follows the divisor), so math.fmod (example) is required for PHP parity — the most important cross-language divergence in this entry.

Rust: the floating % operator already means exactly this (example), no dedicated function needed; Rust applies no fast-math reordering, so the remainder sign is stable.

TypeScript: same as JavaScript — the floating % carries this semantics.