# frozen_string_literal: true # https://rosettacode.org/wiki/Trigonometric_functions#Ruby module RplLang module Core module Trig def populate_dictionary super @dictionary.add_word( ['๐›‘', 'pi'], 'Trig on reals and complexes', '( โ€ฆ -- ๐›‘ ) push ๐›‘', proc do @stack << { type: :numeric, base: 10, value: BigMath.PI( precision ) } end ) @dictionary.add_word( ['sin'], 'Trig on reals and complexes', '( n -- m ) compute sinus of n', proc do args = stack_extract( [%i[numeric]] ) @stack << { type: :numeric, base: infer_resulting_base( args ), value: BigMath.sin( BigDecimal( args[0][:value], precision ), precision ) } end ) @dictionary.add_word( ['asin'], 'Trig on reals and complexes', '( n -- m ) compute arg-sinus of n', proc do run( ' dup abs 1 == ยซ ๐›‘ 2 / * ยป ยซ dup sq 1 swap - sqrt / atan ยป ifte' ) end ) @dictionary.add_word( ['cos'], 'Trig on reals and complexes', '( n -- m ) compute cosinus of n', proc do args = stack_extract( [%i[numeric]] ) @stack << { type: :numeric, base: infer_resulting_base( args ), value: BigMath.cos( BigDecimal( args[0][:value], precision ), precision ) } end ) @dictionary.add_word( ['acos'], 'Trig on reals and complexes', '( n -- m ) compute arg-cosinus of n', proc do run( ' dup 0 == ยซ drop ๐›‘ 2 / ยป ยซ dup sq 1 swap - sqrt / atan dup 0 < ยซ ๐›‘ + ยป ift ยป ifte' ) end ) @dictionary.add_word( ['tan'], 'Trig on reals and complexes', '( n -- m ) compute tangent of n', proc do run( 'dup sin swap cos /' ) end ) @dictionary.add_word( ['atan'], 'Trig on reals and complexes', '( n -- m ) compute arc-tangent of n', proc do args = stack_extract( [%i[numeric]] ) @stack << { type: :numeric, base: infer_resulting_base( args ), value: BigMath.atan( BigDecimal( args[0][:value], precision ), precision ) } end) @dictionary.add_word( ['dโ†’r', 'd->r'], 'Trig on reals and complexes', '( n -- m ) convert degree to radian', proc do run( '180 / ๐›‘ *' ) end ) @dictionary.add_word( ['rโ†’d', 'r->d'], 'Trig on reals and complexes', '( n -- m ) convert radian to degree', proc do run( '๐›‘ 180 / /' ) end) end end end end