rpl.rb/lib/core/trig.rb

89 lines
2.3 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
def pi( stack, dictionary )
stack << { type: :numeric,
base: 10,
2022-02-08 15:45:36 +01:00
value: BigMath.PI( Rpl::Lang.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
end
# sinus
def sinus( stack, dictionary )
2022-02-08 15:45:36 +01:00
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
2022-01-18 17:07:25 +01:00
stack << { type: :numeric,
2022-02-08 15:45:36 +01:00
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
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
def arg_sinus( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, '
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
def cosinus( stack, dictionary )
2022-02-08 15:45:36 +01:00
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
2022-01-18 17:07:25 +01:00
stack << { type: :numeric,
2022-02-08 15:45:36 +01:00
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
end
# arg cosinus
def arg_cosinus( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, '
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
def tangent( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, 'dup sin swap cos /' )
2022-01-18 17:07:25 +01:00
end
# arg tangent
def arg_tangent( stack, dictionary )
2022-02-08 15:45:36 +01:00
stack, args = Rpl::Lang.stack_extract( stack, [%i[numeric]] )
2022-01-18 17:07:25 +01:00
stack << { type: :numeric,
2022-02-08 15:45:36 +01:00
base: Rpl::Lang.infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], Rpl::Lang.precision ), Rpl::Lang.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
end
# convert degrees to radians
def degrees_to_radians( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, '𝛑 180 / *' )
2022-01-18 17:07:25 +01:00
end
# convert radians to degrees
def radians_to_degrees( stack, dictionary )
2022-02-08 15:45:36 +01:00
Rpl::Lang.eval( stack, dictionary, '𝛑 180 / /' )
2022-01-18 17:07:25 +01:00
end
2021-12-02 15:33:22 +01:00
end
end
end