rpl.rb/lib/core/trig.rb
Gwenhael Le Moine 8c5657db42
big refactoring
2022-02-08 15:45:36 +01:00

88 lines
2.3 KiB
Ruby

module Rpl
module Lang
module Core
module_function
# pi constant
def pi( stack, dictionary )
stack << { type: :numeric,
base: 10,
value: BigMath.PI( Rpl::Lang.precision ) }
[stack, dictionary]
end
# sinus
def sinus( stack, dictionary )
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
[stack, dictionary]
end
# https://rosettacode.org/wiki/Trigonometric_functions#Ruby
# arg sinus
def arg_sinus( stack, dictionary )
Rpl::Lang.eval( stack, dictionary, '
dup abs 1 ==
« 𝛑 2 / * »
« dup sq 1 swap - sqrt / atan »
ifte' )
end
# cosinus
def cosinus( stack, dictionary )
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
[stack, dictionary]
end
# arg cosinus
def arg_cosinus( stack, dictionary )
Rpl::Lang.eval( stack, dictionary, '
dup 0 ==
« drop 𝛑 2 / »
«
dup sq 1 swap - sqrt / atan
dup 0 <
« 𝛑 + »
ift
»
ifte' )
end
# tangent
def tangent( stack, dictionary )
Rpl::Lang.eval( stack, dictionary, 'dup sin swap cos /' )
end
# arg tangent
def arg_tangent( stack, dictionary )
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
[stack, dictionary]
end
# convert degrees to radians
def degrees_to_radians( stack, dictionary )
Rpl::Lang.eval( stack, dictionary, '𝛑 180 / *' )
end
# convert radians to degrees
def radians_to_degrees( stack, dictionary )
Rpl::Lang.eval( stack, dictionary, '𝛑 180 / /' )
end
end
end
end