Air mass: Difference between revisions

19,309 bytes added ,  6 months ago
m
(Added Rust solution)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 10 users not shown)
Line 16:
::     it flies in a specially retrofitted Boeing 747 about four flights a week.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V DEG = 0.017453292519943295769236907684886127134
V RE = 6371000
V dd = 0.001
V FIN = 10000000
 
F rho(a)
‘ the density of air as a function of height above sea level ’
R exp(-a / 8500.0)
 
F height(Float a; z, d)
a = altitude of observer
z = zenith angle (in degrees)
d = distance along line of sight
R sqrt((:RE + a) ^ 2 + d ^ 2 - 2 * d * (:RE + a) * cos((180 - z) * :DEG)) - :RE
 
F column_density(a, z)
‘ integrates density along the line of sight ’
V (dsum, d) = (0.0, 0.0)
L d < :FIN
V delta = max(:dd, (:dd) * d)
dsum += rho(height(a, z, d + 0.5 * delta)) * delta
d += delta
R dsum
 
F airmass(a, z)
R column_density(a, z) / column_density(a, 0)
 
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}’)</syntaxhighlight>
 
{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.0000000 1.0000000
5 1.0038096 1.0038096
10 1.0153847 1.0153848
15 1.0351774 1.0351776
20 1.0639905 1.0639909
25 1.1030594 1.1030601
30 1.1541897 1.1541908
35 1.2199808 1.2199825
40 1.3041893 1.3041919
45 1.4123417 1.4123457
50 1.5528040 1.5528102
55 1.7387592 1.7387692
60 1.9921200 1.9921366
65 2.3519974 2.3520272
70 2.8953137 2.8953729
75 3.7958235 3.7959615
80 5.5388581 5.5392811
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 54 ⟶ 212:
}
function max(x,y) { return((x > y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 78 ⟶ 236:
90 34.32981136 34.36666557
</pre>
 
=={{header|Factor}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">global RE, dd, LIM
{{works with|Factor|0.99 2021-02-05}}
RE = 6371000 #Earth radius in meters
<lang factor>USING: formatting io kernel math math.functions math.order
dd = 0.001 #integrate in this fraction of the distance already covered
math.ranges math.trig sequences ;
LIM = 10000000 #integrate only to a height of 10000km, effectively inLIMity
 
print "Angle 0 m 13700 m"
CONSTANT: RE 6,371,000 ! Earth's radius in meters
print "------------------------------------"
CONSTANT: dd 0.001 ! integrate in this fraction of the distance already covered
for z = 0 to 90 step 5
CONSTANT: FIN 10,000,000 ! integrate to a height of 10000km
print rjust(z,2); " "; ljust(airmass(0, z),13,"0"); " "; ljust(airmass(13700, z),13,"0")
next z
end
 
function max(a, b)
! the density of air as a function of height above sea level
: rho ( if a --> xb )then negreturn 8500a /else e^return ;b
end function
 
function rho(a)
! z = zenith angle (in degrees)
#the density of air as a function of height above sea level
! d = distance along line of sight
return exp(-a/8500.0)
! a = altitude of observer
end function
:: height ( a z d -- x )
RE a + :> AA
AA sq d sq + 180 z - deg>rad cos AA * d * 2 * - sqrt RE - ;
 
function height(a, z, d)
:: column-density ( a z -- x )
!#a integrates= along the linealtitude of sightobserver
0#z 0= :>zenith angle (in s! d! degrees)
[ #d FIN= distance along <line ]of [sight
AA = RE + a
dd dd d * max :> delta ! adaptive step size to avoid taking it forever
HH = sqr(AA^2 + d^2 - 2*d*AA*cos(radians(180-z)))
s a z d 0.5 delta * + height rho delta * + s!
return HH - d delta + d!RE
end function
] while s ;
 
function column_density(a, z)
: airmass ( a z -- x )
[#integrates column-density ]along [the dropline 0of column-density ] 2bi / ;sight
sum = 0.0
d = 0.0
while d < LIM
delta = max(dd, (dd)*d) #adaptive step size to avoid it taking forever:
sum += rho(height(a, z, d+0.5*delta)) * delta
d += delta
end while
return sum
end function
 
function airmass(a, z)
"Angle 0 m 13700 m" print
return column_density(a, z) / column_density(a, 0)
"------------------------------------" print
end function</syntaxhighlight>
0 90 5 <range> [
dup [ 0 swap airmass ] [ 13700 swap airmass ] bi
"%2d %15.8f %17.8f\n" printf
] each</lang>
{{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|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">
#define DEG 0.017453292519943295769236907684886127134 'degrees to radians
#define RE 6371000 'Earth radius in meters
Line 183 ⟶ 328:
print using "## ##.######## ##.########";z;airmass(0, z);airmass(13700, z)
next z
</syntaxhighlight>
</lang>
 
{{out}}
Line 210 ⟶ 355:
</pre>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">FUNCTION max(a, b)
IF a > b then LET max = a else LET max = b
END FUNCTION
 
FUNCTION rho(a)
!the density of air as a function of height above sea level
LET rho = exp(-a/8500)
END FUNCTION
 
FUNCTION height(a, z, d)
!a = altitude of observer
!z = zenith angle (in degrees)
!d = distance along line of sight
LET aa = re+a
LET hh = sqr(aa^2+d^2-2*d*aa*cos((180-z)*deg))
LET height = hh-re
END FUNCTION
 
FUNCTION columndensity(a, z)
!integrates density along the line of sight
LET sum = 0
LET d = 0
DO while d < lim
LET delta = max(dd, (dd)*d) !adaptive step size to avoid it taking forever:
LET sum = sum+rho(height(a, z, d+.5*delta))*delta
LET d = d+delta
LOOP
LET columndensity = sum
END FUNCTION
 
FUNCTION airmass(a, z)
LET airmass = columndensity(a, z)/columndensity(a, 0)
END FUNCTION
 
LET deg = .0174532925199433 !degrees to radians
LET re = 6371000 !Earth radius in meters
LET dd = .001 !integrate in this fraction of the distance already covered
LET lim = 10000000 !integrate only to a height of 10000km, effectively infinity
PRINT "Angle 0 m 13700 m"
PRINT "------------------------------------"
FOR z = 0 to 90 step 5
PRINT using "## ##.######## ##.########": z, airmass(0, z), airmass(13700, z)
NEXT z
END</syntaxhighlight>
 
=={{header|C}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
#define DEG 0.017453292519943295769236907684886127134 // degrees to radians
#define RE 6371000.0 // Earth radius in meters
#define DD 0.001 // integrate in this fraction of the distance already covered
#define FIN 10000000.0 // integrate only to a height of 10000km, effectively infinity
 
static double rho(double a) {
// the density of air as a function of height above sea level
return exp(-a / 8500.0);
}
 
static double height(double a, double z, double d) {
// a = altitude of observer
// z = zenith angle (in degrees)
// d = distance along line of sight
double aa = RE + a;
double hh = sqrt(aa * aa + d * d - 2.0 * d * aa * cos((180 - z) * DEG));
return hh - RE;
}
 
static double column_density(double a, double z) {
// integrates density along the line of sight
double sum = 0.0, d = 0.0;
while (d < FIN) {
// adaptive step size to avoid it taking forever
double delta = DD * d;
if (delta < DD)
delta = DD;
sum += rho(height(a, z, d + 0.5 * delta)) * delta;
d += delta;
}
return sum;
}
 
static double airmass(double a, double z) {
return column_density(a, z) / column_density(a, 0.0);
}
 
int main() {
puts("Angle 0 m 13700 m");
puts("------------------------------------");
for (double z = 0; z <= 90; z+= 5) {
printf("%2.0f %11.8f %11.8f\n",
z, airmass(0.0, z), airmass(13700.0, z));
}
}</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|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}}
<syntaxhighlight lang="factor">USING: formatting io kernel math math.functions math.order
math.ranges math.trig sequences ;
 
CONSTANT: RE 6,371,000 ! Earth's radius in meters
CONSTANT: dd 0.001 ! integrate in this fraction of the distance already covered
CONSTANT: FIN 10,000,000 ! integrate to a height of 10000km
 
! the density of air as a function of height above sea level
: rho ( a -- x ) neg 8500 / e^ ;
 
! z = zenith angle (in degrees)
! d = distance along line of sight
! a = altitude of observer
:: height ( a z d -- x )
RE a + :> AA
AA sq d sq + 180 z - deg>rad cos AA * d * 2 * - sqrt RE - ;
 
:: column-density ( a z -- x )
! integrates along the line of sight
0 0 :> ( s! d! )
[ d FIN < ] [
dd dd d * max :> delta ! adaptive step size to avoid taking it forever
s a z d 0.5 delta * + height rho delta * + s!
d delta + d!
] while s ;
 
: airmass ( a z -- x )
[ column-density ] [ drop 0 column-density ] 2bi / ;
 
"Angle 0 m 13700 m" print
"------------------------------------" print
0 90 5 <range> [
dup [ 0 swap airmass ] [ 13700 swap airmass ] bi
"%2d %15.8f %17.8f\n" printf
] each</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|Go}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 264 ⟶ 725:
fmt.Printf("%2d %11.8f %11.8f\n", z, airmass(0, fz), airmass(13700, fz))
}
}</langsyntaxhighlight>
 
{{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|Java}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="java">public class AirMass {
public static void main(String[] args) {
System.out.println("Angle 0 m 13700 m");
System.out.println("------------------------------------");
for (double z = 0; z <= 90; z+= 5) {
System.out.printf("%2.0f %11.8f %11.8f\n",
z, airmass(0.0, z), airmass(13700.0, z));
}
}
 
private static double rho(double a) {
// the density of air as a function of height above sea level
return Math.exp(-a / 8500.0);
}
 
private static double height(double a, double z, double d) {
// a = altitude of observer
// z = zenith angle (in degrees)
// d = distance along line of sight
double aa = RE + a;
double hh = Math.sqrt(aa * aa + d * d - 2.0 * d * aa * Math.cos(Math.toRadians(180 - z)));
return hh - RE;
}
 
private static double columnDensity(double a, double z) {
// integrates density along the line of sight
double sum = 0.0, d = 0.0;
while (d < FIN) {
// adaptive step size to avoid it taking forever
double delta = Math.max(DD * d, DD);
sum += rho(height(a, z, d + 0.5 * delta)) * delta;
d += delta;
}
return sum;
}
private static double airmass(double a, double z) {
return columnDensity(a, z) / columnDensity(a, 0.0);
}
 
private static final double RE = 6371000.0; // Earth radius in meters
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
}</syntaxhighlight>
 
{{out}}
Line 297 ⟶ 830:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def pi: 4 * (1|atan);
 
def radians: . * pi / 180;
Line 321 ⟶ 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 352 ⟶ 885:
"------------------------------------",
( range(0; 91; 5)
| "\(lpad(2)) \(airmass(0; .)|fmt(11;8)) \(airmass(13700; .)|fmt(11;8))" )</langsyntaxhighlight>
{{out}}
<pre>
Line 381 ⟶ 914:
=={{header|Julia}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="julia">using Printf
 
const DEG = 0.017453292519943295769236907684886127134 # degrees to radians
Line 413 ⟶ 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 440 ⟶ 973:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const
Line 479 ⟶ 1,012:
while z <= 90:
echo &"{z:2} {airmass(0, z):11.8f} {airmass(13700, z):11.8f}"
z += 5</langsyntaxhighlight>
 
{{out}}
Line 506 ⟶ 1,039:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say signatures>;
Line 552 ⟶ 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 577 ⟶ 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 613 ⟶ 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 637 ⟶ 1,170:
85 10.07896219 10.08115981
90 34.32981136 34.36666557
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">""" Rosetta Code task: Air_mass """
 
from math import sqrt, cos, exp
 
DEG = 0.017453292519943295769236907684886127134 # degrees to radians
RE = 6371000 # Earth radius in meters
dd = 0.001 # integrate in this fraction of the distance already covered
FIN = 10000000 # integrate only to a height of 10000km, effectively infinity
def rho(a):
""" the density of air as a function of height above sea level """
return exp(-a / 8500.0)
def height(a, z, d):
"""
a = altitude of observer
z = zenith angle (in degrees)
d = distance along line of sight
"""
return sqrt((RE + a)**2 + d**2 - 2 * d * (RE + a) * cos((180 - z) * DEG)) - RE
def column_density(a, z):
""" integrates density along the line of sight """
dsum, d = 0.0, 0.0
while d < FIN:
delta = max(dd, (dd)*d) # adaptive step size to avoid it taking forever:
dsum += rho(height(a, z, d + 0.5 * delta)) * delta
d += delta
return dsum
 
def airmass(a, z):
return column_density(a, z) / column_density(a, 0)
 
print('Angle 0 m 13700 m\n', '-' * 36)
for z in range(0, 91, 5):
print(f"{z: 3d} {airmass(0, z): 12.7f} {airmass(13700, z): 12.7f}")
</syntaxhighlight>{{out}}
<pre>
Angle 0 m 13700 m
------------------------------------
0 1.0000000 1.0000000
5 1.0038096 1.0038096
10 1.0153847 1.0153848
15 1.0351774 1.0351776
20 1.0639905 1.0639909
25 1.1030594 1.1030601
30 1.1541897 1.1541908
35 1.2199808 1.2199825
40 1.3041893 1.3041919
45 1.4123417 1.4123457
50 1.5528040 1.5528102
55 1.7387592 1.7387692
60 1.9921200 1.9921366
65 2.3519974 2.3520272
70 2.8953137 2.8953729
75 3.7958235 3.7959615
80 5.5388581 5.5392811
85 10.0789622 10.0811598
90 34.3298114 34.3666656
</pre>
 
=={{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 678 ⟶ 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 705 ⟶ 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 755 ⟶ 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 780 ⟶ 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 834 ⟶ 1,467:
);
}
}</langsyntaxhighlight>
 
{{out}}
Line 862 ⟶ 1,495:
=={{header|Seed7}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 921 ⟶ 1,554:
writeln(airmass(13700.0, z) digits 8 lpad 17);
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 951 ⟶ 1,584:
{{trans|Go}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension Double {
Line 998 ⟶ 1,631:
 
print(air)
}</langsyntaxhighlight>
 
{{out}}
Line 1,028 ⟶ 1,661:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Math
import "./fmt" for Fmt
 
// constants
Line 1,037 ⟶ 1,670:
 
// The density of air as a function of height above sea level.
var rho = Fn.new { |a| Math.exp(-a/8500).exp }
 
// a = altitude of observer
Line 1,053 ⟶ 1,686:
var d = 0
while (d < FIN) {
var delta = MathDD.max(DD, DD * d) // adaptive step size to avoid it taking forever
sum = sum + rho.call(height.call(a, z, d + 0.5 * delta)) * delta
d = d + delta
Line 1,068 ⟶ 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,097 ⟶ 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,149 ⟶ 1,782:
Z:= Z + 5.;
]
]</langsyntaxhighlight>
 
{{out}}
9,476

edits