Showing posts with label pow. Show all posts
Showing posts with label pow. Show all posts

Tuesday, October 3, 2017

0 - 1 Remapping Functions: Powers 10 to -10

/* remapping functions */



for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   power(i,10) )) ]);
     function power(x,n) = n<0?pow(x,1/abs(min(-1,n-1) )):pow(x,max(1,n+1))   ;


for(i=[-0:1/160:1])translate([i,0])square([1/160,   ( (   power(i,-10) )) ]);
     function power(x,n) = n<0?pow(x,1/abs(min(-1,n-1) )):pow(x,max(1,n+1))   ;
 


Tuesday, May 23, 2017

Round to next Power of 2

Calculate the next power of two higher than or equal to a given number
Usage:


for (i = [0: 100])
  echo(i, ":", next_pow2(i));

function next_pow2(x) = pow(2, ceil(log(x)/log(2))); 

Monday, May 22, 2017

Test if a number is a Fibonacci number

Test if a number is in the list of Fibonacci numbers.
The function is_fibonacci returns a vector [true/false,place],
Usage: 



for (i = [0: 100])
  echo(i, ":", is_fibonacci(i));


function is_fibonacci(N) =
let (
  i = floor(log(N * sqrt(5)) / log((1 + sqrt(5)) / 2) + 0.5),
  n = floor(pow((1 + sqrt(5)) / 2, i) / sqrt(5) + 0.5),
  place = (n == N) ? i : -1)
[(n == N), place];