Air mass: Difference between revisions

7,676 bytes added ,  7 months ago
m
(Air mass in BASIC256 and True BASIC. Bundled [any of the] BASIC languages.)
m (→‎{{header|Wren}}: Minor tidy)
 
(9 intermediate revisions by 5 users not shown)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V DEG = 0.017453292519943295769236907684886127134
V RE = 6371000
V dd = 0.001
Line 51:
print("Angle 0 m 13700 m\n "(‘-’ * 36))
L(z) (0.<91).step(5)
print(f:‘{z:3} {airmass(0, z):12.7} {airmass(13700, z):12.7}’)</langsyntaxhighlight>
 
{{out}}
Line 76:
85 10.0789622 10.0811598
90 34.3298114 34.3666656
</pre>
 
=={{header|Ada}}==
{{trans|C}}
{{works with|Ada 2012}}
<syntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
 
procedure Main is
subtype double is Long_Float;
package double_io is new Ada.Text_IO.Float_IO (double);
use double_io;
package Elementary_Double is new Ada.Numerics.Generic_Elementary_Functions
(Float_Type => double);
use Elementary_Double;
 
-- degrees to radians
Deg : constant := 0.017_453_292_519_943_295_769_236_907_684_886_127_134;
 
-- Earth radius in meters
Re : constant := 6_371_000.0;
 
-- integrate in this fraction of the distance already covered
Dd : constant := 0.001;
 
-- integrate only to a height of 10000km. efectively infinity
Fin : constant := 10_000_000.0;
 
function rho (a : double) return double is (Exp (-a / 8_500.0));
 
function height (a : double; z : double; d : double) return double is
aa : double := Re + a;
hh : double :=
Sqrt (aa * aa + d * d - 2.0 * d * aa * Cos ((180.0 - z) * Deg));
begin
return hh - Re;
end height;
 
function column_density (a : double; z : double) return double is
sum : double := 0.0;
d : double := 0.0;
d_delta : double;
begin
while d < Fin loop
-- adaptive step size to avoid it taking forever
d_delta := Dd * d;
if d_delta < Dd then
d_delta := Dd;
end if;
sum := sum + rho (height (a, z, d + 0.5 * d_delta)) * d_delta;
d := d + d_delta;
end loop;
return sum;
end column_density;
 
function air_mass (a : double; z : double) return double is
(column_density (a, z) / column_density (a, 0.0));
 
z : double := 0.0;
begin
Put_Line ("Angle 0 m 13700 m");
Put_Line ("------------------------------------");
while z <= 90.0 loop
Put(Item => Integer(z), Width => 2);
Put (Item => air_mass (0.0, z), Fore => 8, Aft => 8, Exp => 0);
Put (Item => air_mass (13_700.0, z), Fore => 8, Aft => 8, Exp => 0);
New_Line;
z := z + 5.0;
end loop;
 
end Main;
</syntaxhighlight>
{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.00000000 1.00000000
5 1.00380963 1.00380965
10 1.01538466 1.01538475
15 1.03517744 1.03517765
20 1.06399053 1.06399093
25 1.10305937 1.10306005
30 1.15418974 1.15419083
35 1.21998076 1.21998246
40 1.30418931 1.30419190
45 1.41234169 1.41234567
50 1.55280404 1.55281025
55 1.73875921 1.73876915
60 1.99212000 1.99213665
65 2.35199740 2.35202722
70 2.89531368 2.89537287
75 3.79582352 3.79596149
80 5.53885809 5.53928113
85 10.07896219 10.08115981
90 34.32981136 34.36666557
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f AIR_MASS.AWK
# converted from FreeBASIC
Line 115 ⟶ 212:
}
function max(x,y) { return((x > y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 143 ⟶ 240:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">global RE, dd, LIM
RE = 6371000 #Earth radius in meters
dd = 0.001 #integrate in this fraction of the distance already covered
Line 187 ⟶ 284:
function airmass(a, z)
return column_density(a, z) / column_density(a, 0)
end function</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">
#define DEG 0.017453292519943295769236907684886127134 'degrees to radians
#define RE 6371000 'Earth radius in meters
Line 231 ⟶ 328:
print using "## ##.######## ##.########";z;airmass(0, z);airmass(13700, z)
next z
</syntaxhighlight>
</lang>
 
{{out}}
Line 260 ⟶ 357:
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">FUNCTION max(a, b)
IF a > b then LET max = a else LET max = b
END FUNCTION
Line 303 ⟶ 400:
PRINT using "## ##.######## ##.########": z, airmass(0, z), airmass(13700, z)
NEXT z
END</langsyntaxhighlight>
 
=={{header|C}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 354 ⟶ 451:
z, airmass(0.0, z), airmass(13700.0, z));
}
}</langsyntaxhighlight>
 
{{out}}
Line 380 ⟶ 477:
90 34.32981136 34.36666557
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{trans|GO}}
{{libheader|SysUtils,StdCtrls}}
 
<syntaxhighlight lang="Delphi">
 
const RE = 6371000; { radius of earth in meters}
const DD = 0.001; { integrate in this fraction of the distance already covered}
const FIN = 1e7; { integrate only to a height of 10000km, effectively infinity}
 
function rho(a: double): double;
{ The density of air as a function of height above sea level.}
begin
Result:=Exp(-a / 8500);
end;
 
function Radians(degrees: double): double;
{ Converts degrees to radians}
begin
Result:= degrees * Pi / 180
end;
 
function Height(A, Z, D: double): double;
{ a = altitude of observer}
{ z = zenith angle (in degrees)}
{ d = distance along line of sight}
var AA,HH: double;
begin
AA := RE + A;
HH := Sqrt(AA*AA + D*D - 2*D*AA*Cos(Radians(180-z)));
Result:= HH - RE;
end;
 
function ColumnDensity(A, Z: double): double;
{ Integrates density along the line of sight.}
var Sum,D,Delta: double;
begin
Sum := 0.0;
D := 0.0;
while D < FIN do
begin
delta := Max(DD, DD*D); { adaptive step size to avoid it taking forever}
Sum:=Sum + Rho(Height(A, Z, D+0.5*Delta)) * Delta;
D:=D + delta;
end;
Result:= Sum;
end;
 
 
function AirMass(A, Z: double): double;
begin
Result:= ColumnDensity(A, Z) / ColumnDensity(a, 0);
end;
 
procedure ShowAirMass(Memo: TMemo);
var Z: integer;
begin
Memo.Lines.Add('Angle 0 m 13700 m');
Memo.Lines.Add('------------------------------------');
Z:=0;
while Z<=90 do
begin
Memo.Lines.Add(Format('%2d %11.8f %11.8f', [z, airmass(0, Z), airmass(13700, Z)]));
Z:=Z+5;
end;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.00000000 1.00000000
5 1.00380963 1.00380965
10 1.01538466 1.01538475
15 1.03517744 1.03517765
20 1.06399053 1.06399093
25 1.10305937 1.10306005
30 1.15418974 1.15419083
35 1.21998076 1.21998246
40 1.30418931 1.30419190
45 1.41234169 1.41234567
50 1.55280404 1.55281025
55 1.73875921 1.73876915
60 1.99212000 1.99213665
65 2.35199740 2.35202722
70 2.89531368 2.89537287
75 3.79582352 3.79596149
80 5.53885809 5.53928113
85 10.07896219 10.08115981
90 34.32981136 34.36666557
 
Elapsed Time: 189.304 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
 
<syntaxhighlight lang=easylang>
func rho a .
return pow 2.718281828459 (-a / 8500)
.
func height a z d .
AA = 6371000 + a
HH = sqrt (AA * AA + d * d - 2 * d * AA * cos (180 - z))
return HH - 6371000
.
func density a z .
while d < 10000000
delta = higher 0.001 (0.001 * d)
sum += rho height a z (d + 0.5 * delta) * delta
d += delta
.
return sum
.
func airmass a z .
return density a z / density a 0
.
numfmt 8 2
print "Angle 0 m 13700 m"
print "------------------------"
for z = 0 step 5 to 90
print z & " " & airmass 0 z & " " & airmass 13700 z
.
</syntaxhighlight>
 
=={{header|Factor}}==
{{trans|FreeBASIC}}
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.functions math.order
math.ranges math.trig sequences ;
 
Line 418 ⟶ 646:
dup [ 0 swap airmass ] [ 13700 swap airmass ] bi
"%2d %15.8f %17.8f\n" printf
] each</langsyntaxhighlight>
{{out}}
<pre>
Line 443 ⟶ 671:
90 34.32981136 34.36666557
</pre>
 
 
=={{header|Go}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 498 ⟶ 725:
fmt.Printf("%2d %11.8f %11.8f\n", z, airmass(0, fz), airmass(13700, fz))
}
}</langsyntaxhighlight>
 
{{out}}
Line 527 ⟶ 754:
=={{header|Java}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="java">public class AirMass {
public static void main(String[] args) {
System.out.println("Angle 0 m 13700 m");
Line 570 ⟶ 797:
private static final double DD = 0.001; // integrate in this fraction of the distance already covered
private static final double FIN = 10000000.0; // integrate only to a height of 10000km, effectively infinity
}</langsyntaxhighlight>
 
{{out}}
Line 603 ⟶ 830:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def pi: 4 * (1|atan);
 
def radians: . * pi / 180;
Line 627 ⟶ 854:
else . + "." + "0" * digits
end
| lpad($width);</langsyntaxhighlight>
'''Physics'''
<langsyntaxhighlight lang="jq"># constants
def RE: 6371000; # radius of earth in meters
def DD: 0.001; # integrate in this fraction of the distance already covered
Line 658 ⟶ 885:
"------------------------------------",
( range(0; 91; 5)
| "\(lpad(2)) \(airmass(0; .)|fmt(11;8)) \(airmass(13700; .)|fmt(11;8))" )</langsyntaxhighlight>
{{out}}
<pre>
Line 687 ⟶ 914:
=={{header|Julia}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="julia">using Printf
 
const DEG = 0.017453292519943295769236907684886127134 # degrees to radians
Line 719 ⟶ 946:
@printf("%2d %11.8f %11.8f\n", z, airmass(0, z), airmass(13700, z))
end
</langsyntaxhighlight>{{out}}
<pre>
Angle 0 m 13700 m
Line 746 ⟶ 973:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const
Line 785 ⟶ 1,012:
while z <= 90:
echo &"{z:2} {airmass(0, z):11.8f} {airmass(13700, z):11.8f}"
z += 5</langsyntaxhighlight>
 
{{out}}
Line 812 ⟶ 1,039:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say signatures>;
Line 858 ⟶ 1,085:
for my $z (map{ 5*$_ } 0..18) {
printf "%2d %11.8f %11.8f\n", $z, airmass(0, $z), airmass(13700, $z);
}</langsyntaxhighlight>
{{out}}
<pre>Angle 0 m 13700 m
Line 883 ⟶ 1,110:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">RE</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6371000</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">// radius of earth in meters</span>
<span style="color: #000000;">DD</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.001</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">// integrate in this fraction of the distance already covered</span>
Line 919 ⟶ 1,146:
<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;">"%2d %11.8f %11.8f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">airmass</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">airmass</span><span style="color: #0000FF;">(</span><span style="color: #000000;">13700</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 946 ⟶ 1,173:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">""" Rosetta Code task: Air_mass """
 
from math import sqrt, cos, exp
Line 982 ⟶ 1,209:
for z in range(0, 91, 5):
print(f"{z: 3d} {airmass(0, z): 12.7f} {airmass(13700, z): 12.7f}")
</langsyntaxhighlight>{{out}}
<pre>
Angle 0 m 13700 m
Line 1,009 ⟶ 1,236:
=={{header|Raku}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="raku" perl6line>constant DEG = pi/180; # degrees to radians
constant RE = 6371000; # Earth radius in meters
constant dd = 0.001; # integrate in this fraction of the distance already covered
Line 1,046 ⟶ 1,273:
say join "\n", (0, 5 ... 90).hyper(:3batch).map: -> \z {
sprintf "%2d %11.8f %11.8f", z, airmass( 0, z), airmass(13700, z);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,073 ⟶ 1,300:
=={{header|REXX}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="rexx">/*REXX pgm calculates the air mass above an observer and an object for various angles.*/
numeric digits (length(pi()) - length(.)) % 4 /*calculate the number of digits to use*/
parse arg aLO aHI aBY oHT . /*obtain optional arguments from the CL*/
Line 1,123 ⟶ 1,350:
sum= sum + rho( elev(a, z, d + 0.5*delta) ) * delta; d= d + delta
end /*while*/
return sum</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,148 ⟶ 1,375:
90 │ 34.32981136 34.36666557
─────┴─────────────────────────────────────────────────────────────
</pre>
 
=={{header|RPL}}==
{{trans|FreeBASIC}}
≪ → a ≪ a NEG 8500 / EXP ≫ ≫ ‘'''RHO'''’ STO
≪ ''''RHO'''(√(aa^2+D^2-2*aa*D*COS(180-Z))-re)' EVAL
≫ ‘'''COLD'''’ STO
6371000 3 PICK OVER + → a z re aa
≪ DEG
z 'Z' STO ''''COLD'''' { D 0 1E7 } 1E-7 ∫ DROP
0 'Z' STO ''''COLD'''' { D 0 1E7 } 1E-7 ∫ DROP /
≫ ≫ ‘'''AM'''’ STO
 
≪ { } 0 90 '''FOR''' z z + z '''AM''' + 5 '''STEP''' ≫
{{out}}
<pre>
1: { 0 1
5 1.00380686363
10 1.01537368745
15 1.03515302646
20 1.06394782383
25 1.10299392042
30 1.15409753978
35 1.21985818096
40 1.30403285254
45 1.41214767977
50 1.55256798138
55 1.73847475415
60 1.99177718552
65 2.35157928673
70 2.89478919419
75 3.79512945489
80 5.53784169364
85 10.0771111633
90 34.3235064081 }
</pre>
 
=={{header|Rust}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="rust">const RE: f64 = 6371000.0; // Earth radius in meters
const DD: f64 = 0.001; // integrate in this fraction of the distance already covered
const FIN: f64 = 10000000.0; // integrate only to a height of 10000km, effectively infinity
Line 1,202 ⟶ 1,467:
);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,230 ⟶ 1,495:
=={{header|Seed7}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 1,289 ⟶ 1,554:
writeln(airmass(13700.0, z) digits 8 lpad 17);
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,319 ⟶ 1,584:
{{trans|Go}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension Double {
Line 1,366 ⟶ 1,631:
 
print(air)
}</langsyntaxhighlight>
 
{{out}}
Line 1,396 ⟶ 1,661:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Math
import "./fmt" for Fmt
 
// constants
Line 1,436 ⟶ 1,701:
Fmt.print("$2d $11.8f $11.8f", z, airmass.call(0, z), airmass.call(13700, z))
z = z + 5
}</langsyntaxhighlight>
 
{{out}}
Line 1,465 ⟶ 1,730:
=={{header|XPL0}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight XPL0lang="xpl0">define DEG = 0.017453292519943295769236907684886127134; \degrees to radians
define RE = 6371000.; \Earth radius in meters
define DD = 0.001; \integrate in this fraction of the distance already covered
Line 1,517 ⟶ 1,782:
Z:= Z + 5.;
]
]</langsyntaxhighlight>
 
{{out}}
9,483

edits