Skip to content

Commit

Permalink
Slight adjustment to normalF().
Browse files Browse the repository at this point in the history
This ensures it returns the same min and max results as normal().
  • Loading branch information
tommyettinger committed Oct 2, 2024
1 parent c0e9e44 commit 5df02f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/github/tommyettinger/digital/Distributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* distributions, such as the normal distribution. {@link #probit(double)} and {@link #probitHighPrecision(double)} take
* a double in the 0.0 to 1.0 range (typically exclusive, but not required to be), and produce a normal-distributed
* double centered on 0.0 with standard deviation 1.0 . {@link #normal(long)} takes a long in the entire range of
* possible long values, and also produces a double centered on 0.0 with standard deviation 1.0 . ALl of these ways will
* preserve patterns in the input, so inputs close to the lowest possible input (0.0 for probit(),
* {@link Long#MIN_VALUE} for normal()) will produce the lowest possible output (-8.375 for probit() and normal()),
* possible long values, and also produces a double centered on 0.0 with standard deviation 1.0 . Similarly,
* {@link #normalF(int)} takes an int in the entire range of possible int values, and produces a float centered on 0f
* with standard deviation 1f. All of these ways will preserve patterns in the input, so inputs close to the lowest
* possible input (0.0 for probit(), {@link Long#MIN_VALUE} for normal(), {@link Integer#MIN_VALUE} for normalF()) will
* produce the lowest possible output (-8.375 for probit(), normal(), and normalF()),
* and similarly for the highest possible inputs producing the highest possible outputs.
*/
public final class Distributor {
Expand Down Expand Up @@ -209,7 +211,7 @@ public static float normalF(int n) {
final int top10 = (n >>> 21);
final float t = (n & 0x1FFFFF) * 0x1p-21f, v;
if (top10 == 1023) {
v = t * t * (8.375f - 3.297193345691938f) + 3.297193345691938f;
v = t * t * (8.375005f - 3.297193345691938f) + 3.297193345691938f;
} else {
final float s = TABLE_F[top10];
v = t * (TABLE_F[top10 + 1] - s) + s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ public void testLimits() {
System.out.println("Distributor.normal(1L) == " + Distributor.normal(1L));
System.out.println("Distributor.normal(2L) == " + Distributor.normal(2L));
System.out.println("Distributor.normal(Long.MAX_VALUE) == " + Distributor.normal(Long.MAX_VALUE));

System.out.println("Distributor.normalF(Integer.MIN_VALUE) == " + Distributor.normalF(Integer.MIN_VALUE));
System.out.println("Distributor.normalF(-3) == " + Distributor.normalF(-3));
System.out.println("Distributor.normalF(-2) == " + Distributor.normalF(-2));
System.out.println("Distributor.normalF(-1) == " + Distributor.normalF(-1));
System.out.println("Distributor.normalF(0) == " + Distributor.normalF(0));
System.out.println("Distributor.normalF(1) == " + Distributor.normalF(1));
System.out.println("Distributor.normalF(2) == " + Distributor.normalF(2));
System.out.println("Distributor.normalF(Integer.MAX_VALUE) == " + Distributor.normalF(Integer.MAX_VALUE));
}
}

0 comments on commit 5df02f5

Please sign in to comment.