Horizontal sundial calculations: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 16:
{{trans|Python}}
<
V lng = Float(input(‘Enter longitude => ’))
V ref = Float(input(‘Enter legal meridian => ’))
Line 31:
hra -= lng - ref
V hla = degrees(atan(slat * tan(radians(hra))))
print(‘HR=#3; HRA=#3.3; HLA=#3.3’.format(h, hra, hla))</
{{out}}
Line 61:
{{trans|ALGOL 68}}
sundial.adb:
<
with Ada.Numerics.Elementary_Functions;
procedure Sundial is
Line 105:
end;
end loop;
end Sundial;</
{{out}}
<pre>Enter latitude: -4.95
Line 142:
> Simon
-->
<
REAL lat, slat, lng, ref;
print ( "Enter latitude => " ); read (lat);
Line 165:
new line(stand out)
OD
END</
{{out}}
<pre>
Line 195:
{{trans|F#}}
AutoHotkey is not a command-line programming language, let me make that clear. However, in translating the F# I found that the command line really is best for this type of app. The first 3 comments in the script describe the workarounds used to interface with the commandline.
<
Pi := 4*ATan(1)
,Degrees := Pi/180
Line 226:
FileAppend, %clockhour%`t`t%shr%`t`t%dhla%`n, CONOUT$
}
MsgBox close me when done.</
{{out}}
<pre>Enter Latitude:-4.95
Line 252:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f HORIZONTAL_SUNDIAL_CALCULATIONS.AWK
BEGIN {
Line 270:
function dr(x) { return x * 3.14159265 / 180 } # degrees to radians
function rd(x) { return x * 180 / 3.14159265 } # radians to degrees
</syntaxhighlight>
<p>output:</p>
<pre>
Line 297:
{{works with|QBasic}}
{{trans|ALGOL-68}}
<
10 REM Horizontal sundial calculations
20 DEF FNM(X) = INT(X*1000+0.5)/1000
Line 323:
240 NEXT H
250 END
</syntaxhighlight>
==={{header|BASIC256}}===
<
end
Line 359:
print time; ap$; chr(9); hra; chr(9); chr(9); hla
next hora
end subroutine</
==={{header|True BASIC}}===
<
LET rad2deg = theta*180/PI
END FUNCTION
Line 382:
PRINT USING "## ####.## ####.###": hora, hra, hla
NEXT hora
END</
==={{header|Yabasic}}===
<
end
Line 412:
print time using "##", ap$, chr$(9), hra using "####.##", chr$(9), chr$(9), hla using "####.###"
next hour
end sub</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
INPUT "Enter latitude (degrees) : " latitude
Line 430:
IF ABS(hra) > 90 hla += 180 * SGN(hra * latitude)
PRINT FNusing("##.##", hour), FNusing(" ####.### ", hra), FNusing(" ####.###", hla)
NEXT hour</
{{out}}
(note the correct '''negative''' value for time 18:00)
Line 454:
=={{header|C}}==
{{trans|ALGOL 68}}
<
#include <math.h>
Line 496:
return 0;
}</
=={{header|C sharp|C#}}==
<
namespace RosettaCode
Line 530:
}
}
}</
=={{header|C++}}==
<
#include <iostream>
#include <numbers>
Line 595:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 618:
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
DATA DIVISION.
Line 671:
dial-hour-line-angle-disp
END-PERFORM
.</
{{out}}
Line 700:
=={{header|D}}==
{{trans|Python}}
<
double radians(in double x) pure nothrow { return x * (PI / 180); }
Line 727:
writefln("HR=%3d; HRA=%7.3f; HLA=%7.3f", h, hra, hla);
}
}</
{{out|Example run}}
<pre>Enter latitude => -4.95
Line 756:
=={{header|DWScript}}==
{{trans|Java}}
<
begin
PrintLn(Format('latitude: %7.2f', [lat]));
Line 778:
end;
PrintSundial(-4.95, -150.5, -150);</
{{out}}
<pre>latitude: -4.95
Line 802:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">func getn s$ . v .
write s$
v = number input
Line 821:
hla = atan2 (slat * sin hra) cos hra
print h + 12 & "\t" & hra & "\t\t" & hla
.</
=={{header|ERRE}}==
<
FUNCTION RAD(X)
Line 852:
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 878:
=={{header|Euphoria}}==
{{works with|OpenEuphoria}}
<syntaxhighlight lang="euphoria">
include std/console.e
include std/mathcons.e
Line 914:
if getc(0) then end if
</syntaxhighlight>
{{out}}
<pre>
Line 939:
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<
open System
Line 974:
//To keep the console window open, can be omitted with block comment (" (* comment *) ")
Console.WriteLine("Press any key to continue...")
Console.ReadKey() |> ignore</
{{out|Example output}}
<pre>
Line 1,004:
=={{header|Factor}}==
{{trans|Java}}
<
math.parser math.ranges math.trig sequences ;
IN: rosetta-code.sundial
Line 1,035:
: sundial-demo ( -- ) get-input nl 3dup .diff nl .angles ;
MAIN: sundial-demo</
{{out}}
<pre>
Line 1,062:
=={{header|Forth}}==
<
pad 32 accept pad swap >float 0= throw ;
: >radians ( deg -- rad ) 180e f/ pi f* ;
Line 1,080:
fdup f. ." , "
>radians fsincos fswap frot f* fswap fatan2 >degrees f.
loop fdrop fdrop ;</
=={{header|Fortran}}==
{{works with|gfortran}} with <tt>-fbackslash</tt> option
<
real :: lat, slat, lng, ref
Line 1,128:
end function rd
end program SunDial</
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<
' compile with: fbc -s console
Line 1,166:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> Enter latitude (degrees): -4.95
Line 1,188:
=={{header|FutureBasic}}==
<
def fn rad2deg( theta as double ) as double = theta * 180 / pi
Line 1,212:
fn SolarHourAngle( -4.95, -150.5, -150.0 )
HandleEvents</
Output:
Line 1,240:
=={{header|Go}}==
<
import (
Line 1,272:
fmt.Printf("%2.0f %8.3f %8.3f\n", h, hra, hla)
}
}</
{{out}}
<pre>
Line 1,301:
{{trans|ALGOL 68}}
{{works with|PC-BASIC|any}}
<
10 ' Horizontal sundial calculations
20 PRINT "Enter latitude => ";
Line 1,325:
220 NEXT H%
230 END
</syntaxhighlight>
{{out}}
<pre>
Line 1,352:
=={{header|Haskell}}==
<
roundDec d = (/ 10.0 ^ d) . fromIntegral . round . (* 10.0 ^ d)
Line 1,381:
take 16 (show (roundDec 3 sha) ++ repeat ' ') ++
" " ++ show (roundDec 3 dhla))
[-6,-5 .. 6]</
{{out}}
<pre>*Rosetta.HorSunDial> main
Line 1,405:
=={{header|Icon}} and {{header|Unicon}}==
<
PrintSundial(-4.95, -150.5, -150);
end
Line 1,428:
" HRA=",hra,", HLA=",hla)
}
end</
{{out}}
<pre>latitude: -4.95
Line 1,452:
=={{header|IS-BASIC}}==
<
110 OPTION ANGLE RADIANS
120 TEXT 80
Line 1,467:
230 LET HLA=ATN(S*TAN(HRA*DR))*RD
240 PRINT "HR = ";H,"HRA =";HRA,"HLA =";HLA
250 NEXT</
=={{header|J}}==
<
atan2=: {:@*.@j. NB. arc tangent of y divided by x
Line 1,484:
r=.((,. (,. (atan2 *&slat)/@+.@r.&.rfd)) diff + 15&*) i:6
smoutput lbl ,: ('3.0';'7.3';'7.3') 8!:1 r
)</
{{out|Example}}
<
Latitude _4.95
Longitude _150.5
Line 1,508:
│ 5 │ 75.500 │-18.451 │
│ 6 │ 90.500 │-95.775 │
└──────┴────────────────┴────────────────────┘</
=={{header|Java}}==
{{trans|C}} (Substitutes in atan2 for the hour line angle calculation)
<
public class Sundial {
Line 1,543:
}
}
}</
{{out}}
<pre>Enter latitude: -4.95
Line 1,569:
=={{header|Julia}}==
{{trans|Python}}
<
print("Enter latitude => ")
lat = parse(Float64, readline(STDIN))
Line 1,590:
@printf "HR = %3d; HRA = %7.3f; HLA = %7.3f\n" h hra hla
end
</syntaxhighlight>
{{out}}
Line 1,619:
=={{header|Kotlin}}==
<
import java.lang.Math.cos
import java.lang.Math.sin
Line 1,651:
println("%2d %s %+7.3f %+7.3f".format(hr, am, sha, dhla))
}
}</
Sample input/output:
{{out}}
Line 1,683:
=={{header|Liberty BASIC}}==
Based on Algol & BBC BASIC versions. Note Liberty BASIC works in radians.
<
pi = 3.14159265
input "Enter latitude (degrees) : "; latitude ' -4.95
Line 1,708:
function sgn(x)
if x > 0 then sgn = 1 else sgn = -1
end function</
{{out}}
<pre>
Line 1,733:
=={{header|LiveCode}}==
Translation of BASIC versions.
<
ask "Enter lng,lat,meridian"
if it is empty then exit mouseup
Line 1,760:
function sgn x
if x >0 then return 1 else return -1
end sgn</
=={{header|Logo}}==
<
make "lat readword
type "|Enter longitude: |
Line 1,775:
make "hla arctan product sin :lat quotient sin :hra cos :hra
print (sentence "hour :hour ": :hra ", :hla)
]</
=={{header|Lua}}==
{{trans|Python}}
<
lat = tonumber(io.read())
Line 1,802:
hla = math.deg(math.atan(slat * math.tan(math.rad(hra))))
print(string.format("HR=%3d; HRA=%7.3f; HLA=%7.3f", h, hra, hla))
end</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
lng = Input["longitude", -150.5];
ref = Input["legal meridian", -150];
Line 1,818:
{h, -6, 6}
] // Prepend[{"Hour", "Sun hour angle",
"Dial hour line angle"}] // Grid</
{{out}}
<pre>Hour Sun hour angle Dial hour line angle
Line 1,837:
=={{header|Microsoft Small Basic}}==
{{trans|ALGOL-68}}
<
TextWindow.Write("Enter latitude => ")
lat = TextWindow.ReadNumber()
Line 1,867:
TextWindow.WriteLine("")
EndFor
</syntaxhighlight>
{{out}}
Line 1,893:
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">МГ П2 -> МГ П1 -> МГ sin П0
6 /-/ П3
ИП3 1 5 * ИП1 ИП2 - - П4
tg ИП0 * arctg ИП4 ИП3 С/П
ИП3 1 + П3 7 - x=0 12
Сx С/П</
''Input'': ''latitude'' ^ ''longitude'' ^ ''legal meridian'' С/П ... С/П ...; switch of the angle measure set to ''Г''.
Line 1,909:
{{trans|ALGOL-68}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<
MODULE SunDial;
Line 1,959:
END;
END SunDial.
</syntaxhighlight>
{{out}}
Enter latitude => -4.95
Line 1,985:
=={{header|newLISP}}==
{{trans|Lua}}
<
(define (radians degrees) (mul degrees (div pi 180)))
(define (degrees radians) (mul radians (div 180 pi)))
Line 2,013:
)
(exit)</
=={{header|Nim}}==
{{trans|Python}}
<
let lat = parseFloat readLineFromStdin "Enter latitude => "
Line 2,033:
let hra = float(15 * h) - lng + med
let hla = arctan(slat * tan(hra.degToRad)).radToDeg
echo &"HR={h:3d}; HRA={hra:7.3f}; HLA={hla:7.3f}"</
{{out}}
Line 2,060:
=={{header|Objeck}}==
{{trans|C#}}
<
class Sundial {
function : Main(args : String[]) ~ Nil {
Line 2,086:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
{{trans|ALGOL 68}}
<
let pi = 4. *. atan 1. in
print_endline "Enter latitude => ";
Line 2,113:
Printf.printf "HR= %3d; \t HRA=%7.3f; \t HLA= %7.3f\n" h hra hla;
done
</syntaxhighlight>
{{out}}
<pre>
Line 2,143:
=={{header|Octave}}==
<
lng = input("Enter longitude: ");
ref = input("Enter legal meridian: ");
Line 2,154:
hlas = atand( tand(hras) .* slat );
printf("HR= %3d; \t HRA=%7.3f; \t HLA= %7.3f\n",
[ [-6:6]; hras; hlas] );</
=={{header|OoRexx}}==
{{trans|REXX}}
<
/* Use trigonometric functions provided by rxCalc */
parse arg lat lng mer . /*get the optional arguments from the CL*/
Line 2,195:
sep: say indent copies('-',w1) copies('-',w2) copies('-',w3)
Return
::Requires rxMath Library</
{{out}}
Not the same as for REXX anymore as the REXX computer programming example has been updated.
=={{header|Pascal}}==
<
Const
Line 2,235:
tab, ' HLA= ', hla:7:3)
end
end.</
=={{header|Perl}}==
{{trans|Raku}}
<
binmode STDOUT, ":utf8";
Line 2,263:
printf "%2d %s %8.3f %8.3f\n",
($hour + 12) % 12 || 12, ($hour < 0 ? 'AM' : 'PM'), $sun_deg, $line_deg;
}</
{{out}}
<pre>Enter latitude => -4.95
Line 2,289:
=={{header|Phix}}==
For better behaviour/proper input on pwa/p2js I suppose this should really be a pGUI app.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">prompt</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
Line 2,331:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d:00%s %12.3f %17.3f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">hour12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">am</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ha</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hla</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 2,360:
=={{header|PicoLisp}}==
{{trans|ALGOL 68}}
<
(de prompt (Str . Arg)
Line 2,385:
(align 8 (round Hra))
"; HLA="
(align 8 (round Hla)) ) ) ) ) ) )</
{{out}}
<pre>Enter latitude => -4.95
Line 2,410:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Get-Sundial
{
Line 2,461:
$sundial | Select-Object -Property "Sine of Latitude", "Longitude Difference" | Format-List
$sundial.Hours | Format-Table -AutoSize
</syntaxhighlight>
{{Out}}
<pre>
Line 2,491:
=={{header|PureBasic}}==
{{trans|ALGOL 68}}
<
Define.f lat, slat, lng, ref
Define.i h
Line 2,512:
Next
EndIf</
{{out}}
<pre>Enter latitude => -4.95
Line 2,539:
=={{header|Python}}==
{{trans|ALGOL 68}}
<
import math
try: raw_input
Line 2,559:
hra -= lng - ref
hla = math.degrees(math.atan(slat * math.tan(math.radians(hra))))
print("HR=%3d; HRA=%7.3f; HLA=%7.3f" % (h, hra, hla))</
{{out}}
<pre>
Line 2,592:
Also, I apologize for the length; I added quite a bit of commenting, and I peeled things out into functions so I could test them. Hopefully, the result--though longer--is also more readable.
<
;; print the table for a given latitude and longitude-offset,
Line 2,669:
(check-= (to-hla -30 90) -90 1e-5)
(check < (to-hla -30 91) -90)
</syntaxhighlight>
{{Out}}
<pre>Welcome to DrRacket, version 5.3.3.5--2013-02-20(5eddac74/d) [3m].
Line 2,698:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
sub postfix:<®> ($a) { $a * 180 / pi } # radians to degrees
Line 2,716:
printf "%2d %s %7.3f %7.3f\n",
($hour + 12) % 12 || 12, ($hour < 0 ?? 'AM' !! 'PM'), $sun_deg, $line_deg;
}</
{{out|Example output}}
<pre>
Line 2,751:
No attempt was made to explain the inner workings of the trigonometric functions.
<
numeric digits length( pi() ) - length(.) /*in case sundial is in polar regions. */
parse arg lat lng . /*obtain optional arguments from the CL*/
Line 2,825:
m.=9; numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_ % 2
do j=0 while h>9; m.j= h; h= h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g) *.5; end /*k*/; return g</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,895:
=={{header|Ring}}==
<
# Project : Horizontal sundial calculations
Line 2,920:
see "" + hour + " " + hra + " " + hla + nl
next
</syntaxhighlight>
Output:
<pre>
Line 2,945:
{{trans|ALGOL 68}}
{{trans|Python}}
<
DtoR = PI/180
Line 2,967:
hla = atan( slat * tan( hra * DtoR ))/ DtoR
puts "HR =%3d; HRA =%7.3f; HLA =%7.3f" % [h, hra, hla]
end</
{{out}}
<pre>
Line 2,994:
=={{header|Run BASIC}}==
<
pi = 22 / 7
Line 3,022:
if x >0 then sgn =1 else sgn =-1
end function
end</
{{out}}
<pre>
Line 3,045:
=={{header|Rust}}==
<
use std::io;
struct SundialCalculation {
Line 3,096:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,124:
=={{header|Sather}}==
<
getvalue(s:STR):FLT is
Line 3,155:
end;
end;
end;</
=={{header|Scala}}==
<
import scala.math.{atan2, cos, sin, toDegrees, toRadians}
Line 3,184:
}
}</
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 3,223:
"; HLA= " <& hla digits 3 lpad 7);
end for;
end func;</
{{out}}
<pre>Enter latitude: -4.95
Line 3,249:
=={{header|Sidef}}==
{{trans|Raku}}
<
var longitude = read('Enter longitude => ', Number)
var meridian = read('Enter legal meridian => ', Number)
Line 3,271:
printf("%2d %s %7.3f %7.3f\n",
(hour + 12) % 12 || 12, (hour < 0 ? 'AM' : 'PM'), sun_deg, line_deg)
}</
{{out}}
<pre>
Line 3,298:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<
pi := 1 arcTan * 4.
'Enter latitude: ' display. lat := stdin nextLine asNumber.
Line 3,314:
hla := (hra degreesToRadians tan * slat) arcTan radiansToDegrees.
('HR= %1; %4 HRA=%2; %4 HLA= %3' % { h. hra. hla. $<9> }) displayNl.
]</
=={{header|Tcl}}==
{{trans|ALGOL 68}}
<
fconfigure stdout -buffering none
puts -nonewline "Enter latitude => "; gets stdin lat
Line 3,336:
set hla [expr {atan($slat * tan($hra*$PI/180)) * 180/$PI}]
puts [format "HR=%+3d; HRA=%+8.3f; HLA=%+8.3f" $h $hra $hla]
}</
{{out|Sample output}}
<pre>
Line 3,365:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 3,393:
var hla = (slat*s).atan(c) * 180 / Num.pi
Fmt.print("$2.0f $8.3f $8.3f", h, hra, hla)
}</
{{out}}
Line 3,424:
{{libheader|libc}}
It must be linked with the C standard library and startup code.
<
extern printf, scanf
Line 3,560:
o_ft:
db "HR= %3d; ",9," HRA=%7.3f; ",9," HLA= %7.3f", 10, 0</
=={{header|XBasic}}==
{{trans|ALGOL-68}}
{{works with|Windows XBasic}}
<
PROGRAM "sundial"
VERSION "0.0001"
Line 3,593:
END FUNCTION
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 3,620:
=={{header|XPL0}}==
<
def Pi = 3.14159265358979323846,
Deg2Rad = Pi/180.0,
Line 3,645:
RlOut(0, HA); ChOut(0, Tab); RlOut(0, HLA); CrLf(0);
];
]</
{{out}}
Line 3,671:
=={{header|zkl}}==
{{trans|F#}}
<
//(radian measure)/Degrees => Degree measure
const pi=(0.0).pi, toDeg=(0.0).pi/180;
Line 3,693:
dhla :=(sineLatitude*(shr*toDeg).tan()).atan()/toDeg;
Console.writeln("%s\t\t%5.1f\t\t%+7.3f".fmt(clockHour,shr,dhla));
}</
{{out}}
<pre>
Line 3,726:
=={{header|ZX Spectrum Basic}}==
{{trans|ERRE}}
<
20 DEF FN d(x)=x*180/PI
30 INPUT "Enter latitude (degrees): ";latitude
Line 3,742:
150 IF ABS (hra)>90 THEN LET hla=hla+180*SGN (hra*latitude)
160 PRINT h;" ";hra;" ";hla
170 NEXT h</
|