mourner / suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
BSD 2-Clause "Simplified" License
3.1k stars 419 forks source link

NaN invalid arguments to acos() #134

Closed craigmurray100 closed 5 years ago

craigmurray100 commented 5 years ago

For some lat/long pairs the hourAngle calculation throws NaN because a call to acos() has out of range input. Example:

var date = new Date("Wed, 22 May 2019 01:07:16 GMT");
var lat = 65;
var lng = 117.547923;

var times = SunCalc.getTimes(date, lat, lng);

for(var time in times){
    var t = times[time];
    console.log(time + " : " + t.toLocaleString());
}

result: solarNoon : 5/21/2019, 11:07:51 PM nadir : 5/21/2019, 11:07:51 AM sunrise : 5/21/2019, 1:24:18 PM sunset : 5/22/2019, 8:51:24 AM sunriseEnd : 5/21/2019, 1:33:37 PM sunsetStart : 5/22/2019, 8:42:06 AM dawn : Invalid Date dusk : Invalid Date nauticalDawn : Invalid Date nauticalDusk : Invalid Date nightEnd : Invalid Date night : Invalid Date goldenHourEnd : 5/21/2019, 3:00:28 PM goldenHour : 5/22/2019, 7:15:14 AM

craigmurray100 commented 5 years ago

This was my misunderstanding. Invalid Date will result from a location for which some of the times do not exist. At extreme latitude not all time-spans will be present.