rpl.rb/lib/core/trig.rb

81 lines
1.7 KiB
Ruby
Raw Normal View History

2022-02-10 14:50:59 +01:00
# frozen_string_literal: true
2021-12-02 15:33:22 +01:00
2022-02-10 14:50:59 +01:00
module Lang
module Core
module_function
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# pi constant
def pi
@stack << { type: :numeric,
base: 10,
value: BigMath.PI( precision ) }
end
# sinus
def sinus
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], precision ), precision ) }
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# https://rosettacode.org/wiki/Trigonometric_functions#Ruby
# arg sinus
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-02-10 14:50:59 +01:00
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# cosinus
def cosinus
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], precision ), precision ) }
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# arg cosinus
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-02-10 14:50:59 +01:00
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# tangent
def tangent
run( 'dup sin swap cos /' )
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# arg tangent
def arg_tangent
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], precision ), precision ) }
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# convert degrees to radians
def degrees_to_radians
run( '180 / 𝛑 *' )
end
2022-01-18 17:07:25 +01:00
2022-02-10 14:50:59 +01:00
# convert radians to degrees
def radians_to_degrees
run( '𝛑 180 / /' )
2021-12-02 15:33:22 +01:00
end
end
end