rpl.rb/lib/core/trig.rb

110 lines
2.9 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,
value: BigMath.PI( Rpl::Lang::Core.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
end
# sinus
def sinus( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[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 )
# # Handle angles with no tangent.
# return -PI / 2 if y == -1
# return PI / 2 if y == 1
# # Tangent of angle is y / x, where x^2 + y^2 = 1.
# atan(y / sqrt(1 - y * y, prec), prec)
2022-01-18 22:30:16 +01:00
stack << { value: '
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 »
ifte',
2022-01-18 17:07:25 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# cosinus
def cosinus( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
end
# arg cosinus
def arg_cosinus( stack, dictionary )
2022-01-20 10:55:13 +01:00
stack << { value: '
dup 0 ==
« drop 𝛑 2 / »
2022-01-20 10:55:13 +01:00
«
dup sq 1 swap - sqrt / atan
dup 0 <
« 𝛑 + »
ift
»
ifte',
2022-01-18 17:07:25 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# tangent
def tangent( stack, dictionary )
stack << { value: 'dup sin swap cos /',
2022-01-18 17:07:25 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# arg tangent
def arg_tangent( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
end
# convert degrees to radians
def degrees_to_radians( stack, dictionary )
stack << { value: '𝛑 * 180 /',
2022-01-18 17:07:25 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# convert radians to degrees
def radians_to_degrees( stack, dictionary )
stack << { value: '180 * 𝛑 /',
2022-01-18 17:07:25 +01:00
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
2021-12-02 15:33:22 +01:00
end
end
end