Author Topic: Supported math constants and functions for doubles  (Read 1346 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Supported math constants and functions for doubles
« on: November 07, 2017, 08:05:52 AM »
These are two small programs which deal with doubles.

The first one is intended to test Oxygen's built-in constants and functions. I do not know if I missed something, but comparing with the solutions at RosettaCode.org I think that Oxygen has provided a full set for all situations. The variables n1-n12, e1-e12 can be modified to see the effects.
Code: OxygenBasic
  1. 'http://www.rosettacode.org/wiki/Real_constants_and_functions
  2. 'Constants and functions to handle doubles
  3.  
  4. include "$/inc/console.inc"
  5.  
  6. % e=exp(1)    ' Euler's number e
  7.  
  8. double n1,ne,n2,n3,n4,n5,n6,n6a,n7,n7a,n8,n9,n10,n11,n12
  9. string e1,ee,e2,e3,e4,e5,e6,e6a,e7,e7a,e8,e9,e10,e11,e12
  10.  
  11. ' square root
  12. n1=2 : e1="2"          
  13. ' natural logarithm
  14. ne=2 : ee="2"
  15. ' log2
  16. n2=256 : e2="256"        
  17. ' log10
  18. n3=5 : e3="5"        
  19. ' logn
  20. n4=4096 : e4="4096"      
  21. ' logn base
  22. n5=16 : e5="16"    
  23. ' power, value
  24. n6=9 : e6="9"
  25. ' power, exponent
  26. n6a=4 : e6a="4"
  27. ' root
  28. n7a=4 : e7a="4"
  29. n7=6561 : e7="6561"
  30. ' exp value for e
  31. n8=log10(10) : e8="log10(10)"  
  32. ' pow e, value for exponent
  33. n9=log(5) : e9="log(5)"    
  34. ' pow 10, value for exponent
  35. n10=log10(5) : e10="log10(5)"  
  36. ' abs, floor, ceil
  37. n11=-1.2 : e11="-1.2"  
  38. ' round, trunc, frac
  39. n12=-1.8 : e12="-1.8"    
  40. -----------------------------------
  41.  
  42. SetConsoleTitle "Constants and functions dealing with doubles"
  43. printl "e ->  %e=exp(1) = " tab e                        
  44. printl "pi -> pi = " tab tab pi                        
  45. printl
  46. printl "square root of " e1 " -> sqrt("e1") =  " tab tab sqrt(n1)          
  47. printl
  48. printl "natural logarithm of e -> log(e) = " tab tab log(e)
  49. printl "natural logarithm of "ee" -> log("ee") = " tab tab log(ne)            
  50. printl "base 2 logarithm of " e2 " -> log2("e2") = " tab tab log2(n2)          
  51. printl "base 10 logarithm of " e3 " -> log10("e3") = " tab tab log10(n3)                
  52. printl "base "e5" logarithm of " e4 " -> logn("e4","e5") = " tab logn(n4,n5)
  53. printl                  
  54. printl "" e6 " to the power of " e6a " -> pow("e6","e6a") = " tab tab pow(n6,n6a)        
  55. printl "" e6 " to the power of " e6a " -> " e6"^"e6a " = " tab tab tab n6^n6a            
  56. printl " " e7a ". root of " e7 " -> pow("e7",1/"e7a") = " tab tab pow(n7,1/n7a)
  57. printl
  58. printl "e to the power of " e8 " -> exp("e8") = " tab  exp(n8)      "   -- expon. value of " n8
  59. printl "e to the power of "e9"  -> pow(e,"e9") = " tab tab str(pow(e,n9),5)  "   -- pow(e, "str(n9,5)")"
  60. printl "10 to the power of "e10" -> pow(10,"e10") = " tab str(pow(10,n10),5)    "   -- pow(10, "str(n10,5)")"
  61. printl
  62. printl "absolute value of " e11 " -> abs("e11") = " tab tab tab abs(n11)          
  63. printl "floor of    " e11 " -> floor("e11") = " tab tab tab floor(n11)        
  64. printl "floor of    " (-val(e11)) " -> floor("(-val(e11))") = " tab tab tab floor(-n11)
  65. printl "ceiling of  " e11 " -> ceil("e11") =  " tab tab tab ceil(n11)
  66. printl "ceiling of  " (-val(e11)) " -> ceil("(-val(e11))") =  " tab tab tab ceil(-n11)          
  67. printl "rounded of  " e12 " -> round("e12") = " tab tab tab round(n12)
  68. printl "rounded of  " (-val(e12)) " -> round("(-val(e12))") = " tab tab tab round(-n12)
  69. printl "truncate of " e12 " -> trunc("e12") = " tab tab tab trunc(n12)
  70. printl "truncate of " (-val(e12)) " -> trunc("(-val(e12))") = " tab tab tab trunc(-n12)        
  71. printl "fraction of " e12 " -> frac("e12") =  " tab tab tab frac(n12)
  72. printl "fraction of " (-val(e12)) " -> frac("(-val(e12))") =  " tab tab tab frac(-n12)
  73. printl
  74. printl "Enter ..." : waitkey
  75.  

The second program lists the trigonometric functions. Here n1 and the select case values can be modified to test different results.
Code: OxygenBasic
  1. 'http://www.rosettacode.org/wiki/Trigonometric_functions
  2. 'https://en.wikipedia.org/wiki/List_of_trigonometric_identities
  3. 'hyperbolic functions, see:
  4. 'Oxygenbasic/inc/Mathcomplex.inc
  5. 'OxygenBasic/examples/Math/TrigPlus.o2bas
  6.  
  7. include "$/inc/console.inc"
  8.  
  9. double d, r, ny, nx, n1
  10. n1=1           'radians, degrees
  11.  
  12. SetConsoleTitle "Trigonometric Functions"
  13. printl str(n1,7)" deg = (pi/180*"str(n1,7)") -> rad("str(n1,7)") = " rad(n1) " rad(ian)"
  14. printl str(n1,7)" rad = (180/pi*"str(n1,7)") -> deg("str(n1,7)") = " deg(n1) " deg(ree)"
  15. printl "180 deg -> rad(180) = " rad(180) " rad"
  16. printl " pi rad -> deg(pi)  = " deg(pi)  " deg"
  17. printl
  18.  
  19. for x = 0 to 3600    'angle*10
  20.  select case x
  21.   case 0,450,1950
  22. '  case 300,600,900
  23. '  case 1200,1500,1800
  24.    d=x/10             'degrees
  25.    r = rad(d)         'radians
  26.  
  27.     printl "sin(rad) -- Sine : " tab d " degrees (or " str(r,7) " radians) is " str(sin(r),7)
  28.     printl "cos(rad) -- Cosine: " tab d " degrees (or " str(r,7) " radians) is " str(cos(r),7)
  29.     printl "tan(rad) -- Tangent: " tab d " degrees (or " str(r,7) " radians) is " str(tan(r),7)
  30.     printl "asin(y/r) -- Arc Sine:" tab tab str(sin(r),7) " is " str(deg(asin(sin(r))),7) " degrees (or " str(asin(sin(r)),7) " radians)"
  31.     printl "acos(x/r) -- Arc CoSine:" tab str(cos(r),7) " is " str(deg(acos(cos(r))),7) " degrees (or " str(acos(cos(r)),7) " radians)"
  32.     printl "atn(y/x) --  Arc Tangent:" tab str(tan(r),7) " is " str(deg(atn(tan(r))),7) " degrees (or " str(atn(tan(r)),7) " radians)"
  33.     printl
  34.   end select
  35. next x
  36. printl
  37. for y = -1 to 1
  38.   for x = -1 to 1
  39.      ny=y : nx=x
  40.      printl "arctan2 of y="str(ny,7)" and x="str(nx,7)" -> atan("str(ny,7)","str(nx,7) ") = " _
  41.         str(atan(ny,nx),7) " radians or " str(deg(atan(ny,nx)),7) " deg"
  42.   next x
  43. next y
  44.  
  45. printl
  46. printl "Enter ... " : waitkey
  47.  

I did not find built-in hyperbolic functions, but they can be derived from the existing ones. And of course Charles has this already done in Oxygenbasic/inc/Mathcomplex.inc and in OxygenBasic/examples/Math/TrigPlus.o2bas.

Roland


.