20
4
Introduction
tl;dr
Continuously output the current distance from the Earth to the Sun.
Simplified, the orbit of the Earth around the Sun is an ellipse. So the actual distance between both is constantly changing. This distance can be calculated for any given day using this formula:
The equation can be split into the following parts2:
1
represents 1 AU (astronomical unit), equals149,597,870.691 km
0.01672
is the orbital eccentricity between the Earth and the Suncos
is of course the cosine function, but with argument in degrees rather than radians0.9856
is 360° / 365.256363 days, a full rotation in one year, where365.256363
is the length of a sidereal year, in mean solar daysday
is the day of the year[1-365]
4
represents the offset to the perihelion, which is between 4th and 6th of January
The formula takes a whole day but for the purpose of this challenge – a continuously output – you have to be more accurate; or nothing much will happen till the next day. Simply add the percentage of the past time to the current day, like1:
day + (h * 3600 + m * 60 + s) / 864 / 100
A few Examples:
- 1 January, 23:59:59
1.99998842592593
- 1 January, 18:00:00
1.75
- 1 January, 12:00:00
1.50
- 1 January, 06:00:00
1.25
Input
This challenge has no input.
If your language can't get the current time, you can get it as an input to your program. Valid inputs are timestamps or complete date-time strings that suits the language best. Passing the current day alone (like 5
for 5th January or 5.25
for the same day at 6 o'clock) is not allowed.
Output
Output the current distance from the Earth to the Sun:
- Output the value in
km
. - Update the value at least every second.
Example output:
152098342
If it doesn't increase your byte count, you can also pretty print the result:
152,098,342
152,098,342 km
Requirements
- You can write a program or a function. If it is an anonymous function, please include an example of how to invoke it.
- This is code-golf so shortest answer in bytes wins.
- Standard loopholes are disallowed.
Example implementation
I've prepared an example implementation in JavaScript. It's neither competitive nor golfed.
// dayOfYear from http://stackoverflow.com/a/8620357/1456376
Date.prototype.dayOfYear = function() {
var j1= new Date(this);
j1.setMonth(0, 0);
return Math.round((this-j1)/8.64e7);
}
// vars
var e = document.getElementById('view'),
au = 149597870.691,
deg2rad = Math.PI/180,
date = now = value = null;
// actual logic
function calculate() {
date = new Date();
now = date.dayOfYear() + (date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds()) / 864 / 100;
value = 1 - 0.01672 * Math.cos(deg2rad * 0.9856 * (now - 4));
// supported in Firefox and Chrome, unfortunately not in Safari
e.innerHTML = Math.round(value * au).toLocaleString('en-US') + ' km';
setTimeout(calculate, 1000);
}
// let's do this
calculate();
<div id="view"></div>
1 To not unreasonably increase complexity, you don't have to convert your local time to UTC. If you use UTC please add a note to your answer.
2 For more details see "Earth-Sun distance on a given day of the year" over at Physics
What should programming languages do that cannot access the current time? Like BF etc? – flawr – 2016-01-25T09:52:34.447
3I believe your example is incorrect, since
Math.cos
uses radians. And since this formula seems very approximate, you'll have to be clear on how answers are to be verified. – grc – 2016-01-25T10:06:38.120@grc I've fixed the error in my example - thanks for pointing me to it. – insertusernamehere – 2016-01-25T16:52:45.053
@flawr You can get the time as an input to your program. The question is updated accordingly. – insertusernamehere – 2016-01-25T16:58:15.220
This challenge was discussed on meta. It's back with some improvements now.
– insertusernamehere – 2016-01-25T16:58:20.537I think your estimate of the length of a year is slightly high. – SuperJedi224 – 2016-01-25T17:09:10.447
What formats are allowed for the time input, for languages that cannot query the current time? – Mego – 2016-01-25T17:46:26.113
What do we do on leap years? – lirtosiast – 2016-01-25T18:15:25.893
@ThomasKwa Not much I'd say, the year has just 366 days then. – insertusernamehere – 2016-01-25T18:31:01.023
Can I use an alternate, more accurate formula? – busukxuan – 2016-01-26T06:31:33.317
@busukxuan Yes you can. I'd like to see another approach. Do you want to use Kepler's equation? If you add an answer, it would be great to explain how it works or differ from this approach as well – so that everybody learns something. Your answer will not be competitive though as it will differ from the rules of the challenge. This means you can receive upvotes for your answer but you can't win, even if you have the lowest byte count. – insertusernamehere – 2016-01-26T09:59:04.557
@insertusernamehere I just posted my answer. The formula I used is basically the same as yours, only with different constants in the cos, and seconds rather than days. Is it still non-competing? If it is, I will strikethrough the "(probably)" in the title. – busukxuan – 2016-01-26T11:02:53.983
@busukxuan Ah I see how you solved it. You adjusted the values to get a more accurate value I think this is fine. :) – insertusernamehere – 2016-01-26T12:14:39.247
@insertusernamehere Oh ok thank you. I guess now I will strikethrough "(probably) non-" instead :-) – busukxuan – 2016-01-26T12:29:22.430
Are six digits of accuracy sufficient? – lirtosiast – 2016-01-26T23:52:53.807
1I bet Mathematica has a built-in for it! – sergiol – 2018-04-11T13:27:04.573