Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/kosherjava/zmanim/util/SPACalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ private double getUTCSunRiseSet(LocalDate localDate, GeoLocation geoLocation, do
}
}
double adjustedZenith = adjustZenith(zenith, elevation, localDate);
if (!Double.isFinite(adjustedZenith)) {
// A NaN or infinite zenith (or a NaN / infinite refraction, solar radius or earth radius, which make the adjusted
// zenith NaN) has no crossing, and the bracketing search can't rule out any interval of a NaN function.
return Double.NaN;
}
double riseSet = solveRiseSet(localDate, geoLocation, adjustedZenith, solarEvent);
if (Double.isNaN(riseSet)) {
return Double.NaN;
Expand Down Expand Up @@ -654,6 +659,11 @@ private static final class CrossingSearch {
* @return the crossing time, or NaN if the interval holds none
*/
double find(double start, double end, double a, double b) {
if (Double.isNaN(a) || Double.isNaN(b)) {
// No interval of a NaN function can be discarded, so without this the search would split all the way down to
// TIME_TOLERANCE in every branch (about 2^27 evaluations).
return Double.NaN;
}
double width = end - start;
double slope = (b - a) / width;
double slopeError = curvature * width / 2.0 + 2.0 * ROUNDING_ERROR / width;
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/kosherjava/zmanim/util/SPACalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,28 @@ public void defaultsAndSetters() {
public void getCalculatorName() {
assertEquals("NREL Solar Position Algorithm", calculator().getCalculatorName());
}

/**
* A NaN target zenith (from a NaN / infinite zenith, refraction or solar radius, or a negative / NaN earth radius) used to
* make the bracketing rise / set search run for hours. It must return NaN like the other calculators.
*/
@Test(timeout = 10_000)
public void nonFiniteZenithReturnsNaNPromptly() {
GeoLocation location = new GeoLocation("Lakewood, NJ", 40.0721087, -74.2400243, 5017, ZoneId.of("America/New_York"));
LocalDate date = LocalDate.of(2026, 1, 1);
assertTrue(Double.isNaN(calculator().getUTCSunrise(date, location, Double.POSITIVE_INFINITY, false)));
assertTrue(Double.isNaN(calculator().getUTCSunset(date, location, Double.NaN, false)));

SPACalculator negativeEarthRadius = calculator();
negativeEarthRadius.setEarthRadius(-6371);
assertTrue(Double.isNaN(negativeEarthRadius.getUTCSunrise(date, location, 90.0, true)));

SPACalculator nanRefraction = calculator();
nanRefraction.setRefraction(Double.NaN);
assertTrue(Double.isNaN(nanRefraction.getUTCSunset(date, location, 90.0, false)));

SPACalculator infiniteSolarRadius = calculator();
infiniteSolarRadius.setSolarRadius(Double.POSITIVE_INFINITY);
assertTrue(Double.isNaN(infiniteSolarRadius.getUTCSunrise(date, location, 90.0, false)));
}
}
Loading