! Math utils

Post Reply
User avatar
Dutchman
Posts: 148
Joined: Tue Aug 06, 2019 4:47 pm
Location: Netherlands

! Math utils

Post by Dutchman » Wed Nov 20, 2019 12:19 pm

! Math utils {/Extensions/Math/! Math utils}
Contains some missing math functions

Code: Select all

' {/Extensions/Math/! Math utils}
[ missing mathematical functions, initially at October 2019]
'
[––––––––––––––––––––––– Functions –––––––––––––––––––––––––
Math_Truncate(n)
returns n rounded towards zero
–––
Math_Crop(n,digits)
returns n with mantissa trimmed to 'digits' numbers
–––
-------------- OBSOLETE --------------
Odd(n) superseded by Version 1.6
returns 0 if n is non-integer or n is even
else returnvalue is 1
–––
Even(n) superseded by Version 1.6
returns 0 if n is non-integer or n is even
else returnvalue is 1
–––
End of function description –––––––––––]
'
[–––––––––––––––––––––– Implementation ––––––––––––––––––––––]
DEF Math_Truncate(n) ' by Ton Nillesen, November 2019
IF n<0 THEN RETURN CEIL(n) ELSE RETURN FLOOR(n)
END DEF
'
DEF Math_Crop(n,digits) ' by Ton Nillesen, November 2019
IF ABS(n)<10^(-digits) THEN RETURN 0
'--- normalise
pwr=CEIL(LOG10(ABS(n)))
n*=1/10^pwr
'--- round
m=10^digits
n=INT(m*n+0.5)/m
'--- restore
n*=10^pwr
RETURN n
END DEF
'
[––––––––––––––––––––––––– Obsolete –––––––––––––––––––––––––
DEF Odd(n) ' by Ton Nillesen, November 2019
  IF (ABS(n)-INT(ABS(n)))>0 THEN RETURN 0
  IF INT(n/2)=n/2 THEN RETURN 0 ELSE RETURN 1
END DEF
'
DEF Even(n) ' by Ton Nillesen, November 2019
  IF (ABS(n)-INT(ABS(n)))>0 THEN RETURN 0
  IF INT(n/2)=n/2 THEN RETURN 1 ELSE RETURN 0
END DEF
'
end obsolete ]
# Odd Even Crop Integer.bb is a test and demo program

UPDATES
20191125
Odd() and Even() are now obsolete, supersede by MATH ODD and MATH EVEN
Truncate() and Crop() renamed to Math_Truncate() and Math_Crop()
It is still a long way to go

Post Reply