rpl.rb/lib/core/trig.rb

81 lines
1.7 KiB
Ruby
Raw Normal View History

2021-12-02 15:33:22 +01:00
module Rpl
2022-01-18 17:07:25 +01:00
module Lang
module Core
module_function
2021-12-02 15:33:22 +01:00
2022-01-18 17:07:25 +01:00
# pi constant
2022-02-10 14:33:09 +01:00
def pi
@stack << { type: :numeric,
base: 10,
value: BigMath.PI( precision ) }
2022-01-18 17:07:25 +01:00
end
# sinus
2022-02-10 14:33:09 +01:00
def sinus
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:33:09 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], precision ), precision ) }
2022-01-18 17:07:25 +01:00
end
2022-01-18 22:30:16 +01:00
# https://rosettacode.org/wiki/Trigonometric_functions#Ruby
2022-01-18 17:07:25 +01:00
# arg sinus
2022-02-10 14:33:09 +01:00
def arg_sinus
run( '
2022-01-18 17:07:25 +01:00
dup abs 1 ==
2022-01-20 10:55:13 +01:00
« 𝛑 2 / * »
2022-01-18 17:07:25 +01:00
« dup sq 1 swap - sqrt / atan »
2022-02-08 15:45:36 +01:00
ifte' )
2022-01-18 17:07:25 +01:00
end
# cosinus
2022-02-10 14:33:09 +01:00
def cosinus
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:33:09 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], precision ), precision ) }
2022-01-18 17:07:25 +01:00
end
# arg cosinus
2022-02-10 14:33:09 +01:00
def arg_cosinus
run( '
2022-01-20 10:55:13 +01:00
dup 0 ==
« drop 𝛑 2 / »
2022-01-20 10:55:13 +01:00
«
dup sq 1 swap - sqrt / atan
dup 0 <
« 𝛑 + »
ift
»
2022-02-08 15:45:36 +01:00
ifte' )
2022-01-18 17:07:25 +01:00
end
# tangent
2022-02-10 14:33:09 +01:00
def tangent
run( 'dup sin swap cos /' )
2022-01-18 17:07:25 +01:00
end
# arg tangent
2022-02-10 14:33:09 +01:00
def arg_tangent
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:33:09 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], precision ), precision ) }
2022-01-18 17:07:25 +01:00
end
# convert degrees to radians
2022-02-10 14:33:09 +01:00
def degrees_to_radians
run( '180 / 𝛑 *' )
2022-01-18 17:07:25 +01:00
end
# convert radians to degrees
2022-02-10 14:33:09 +01:00
def radians_to_degrees
run( '𝛑 180 / /' )
2022-01-18 17:07:25 +01:00
end
2021-12-02 15:33:22 +01:00
end
end
end