In C#, Math.BitDecrement(x) returns the next double belowx. It moves one gap down on the double "ruler", where gaps are the smallest steps a double can take. For 1.0 the next number down is 0.9999999999999999.
The most useful trick is to find the largest number that is still smaller than a limit. If a value must be "less than 1" (not "less than or equal"), then Math.BitDecrement(1.0) is as close to 1 as you may go. Random-number code and range checks sometimes need exactly this. It is the mirror image of BitIncrement(), and it is an advanced tool for beginners to know about rather than use daily.
This is a static member of the static class System.Math. You call it with the class name, such as Math.BitDecrement(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.BitDecrement was added in .NET Core 3.0. It is not in classic .NET Framework.
Syntax
Pass a double. You get the next double that is smaller than it.
Returns -double.Epsilon, the tiniest negative double.
Math.BitDecrement(double.MinValue)
Returns -Infinity.
Math.BitDecrement(double.NaN)
Returns NaN.
The result is always smaller than the input, unless the input is negative infinity or NaN (returned as they are). For positive numbers it moves toward zero, and for negative numbers it moves away from zero. BitDecrement(BitIncrement(x)) brings you back to x. The step size depends on how large the number is.
Library declarations
C#
public static double BitDecrement(double x);
Parameters
Parameter
Description
x
Required. The starting number.
Return Value
Return Value
Description
double
The largest double that is smaller than x. -Infinity for double.MinValue, double.MaxValue for positive infinity, and NaN or negative infinity unchanged.
C# Examples
Example 1 — The Next Double Below 1.0
Call BitDecrement on 1.0 and check that it undoes BitIncrement.
C#
using System;
class Program {
static void Main() {
double below = Math.BitDecrement(1.0);
Console.WriteLine(below);
Console.WriteLine(below < 1.0);
Console.WriteLine(Math.BitDecrement(Math.BitIncrement(1.0)) == 1.0);
}
}
Output
0.9999999999999999
True
True
How It Works
The next mark down. The closest double below 1.0 is 0.9999999999999999. Nothing can be stored between it and 1.0.
Smaller.below < 1.0 is True. The step always goes down.
A round trip.BitIncrement(1.0) goes one step up and BitDecrement goes one step back down. You are at 1.0 again, so the result is True.
Gap below versus above. The step below 1.0 is half the size of the step above 1.0, because the stored exponent changes at 1.0.
Example 2 — The Largest Number Below a Limit
Use BitDecrement to find the largest double that is strictly below 1 and below 10.
C#
using System;
class Program {
static void Main() {
double almostOne = Math.BitDecrement(1.0);
Console.WriteLine(Math.Floor(almostOne));
Console.WriteLine(Math.Floor(1.0));
double below10 = Math.BitDecrement(10.0);
Console.WriteLine(below10 < 10.0);
Console.WriteLine(below10 == 10.0);
}
}
Output
0
1
True
False
How It Works
Just under 1.almostOne is the biggest double that is still below 1. It is so close that you might think it is 1.
Floor tells the truth.Math.Floor(almostOne) is 0, because the number really is below 1. Math.Floor(1.0) is 1.
Just under 10.below10 is the neighbour of 10 on the lower side. It is smaller, so below10 < 10.0 is True.
Not equal. Even though the two are one step apart, below10 == 10.0 is False. This is how a "less than 10, never 10" limit can be built.
Example 3 — Edges: Zero, MinValue, Infinity, and NaN
See what happens at zero, at the lowest double, at positive infinity, and for NaN.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.BitDecrement(0.0) == -double.Epsilon);
Console.WriteLine(Math.BitDecrement(double.MinValue));
Console.WriteLine(Math.BitDecrement(double.PositiveInfinity) == double.MaxValue);
Console.WriteLine(Math.BitDecrement(double.NaN));
}
}
Output
True
-Infinity
True
NaN
How It Works
Below zero. The next number down from 0 is -double.Epsilon, the tiniest negative double. The comparison prints True.
Past the bottom. There is no double below double.MinValue except negative infinity, so you get -Infinity.
From infinity. The next number down from positive infinity is the biggest real double, double.MaxValue.
NaN. "Not a number" has no neighbour, so it is returned unchanged. The method does not throw.
Frequently Asked Questions
It returns the next representable double below the given number, the smallest possible step down. Math.BitDecrement(1.0) is 0.9999999999999999.
Use Math.BitDecrement(1.0). It gives 0.9999999999999999, which is strictly less than 1.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework.
BitDecrement always moves exactly one step down. Subtracting a small number may do nothing (if it is smaller than half the gap) or move several steps, depending on the size of the starting number.
No. Math is a static class, so you call Math.BitDecrement(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.