-1

I need to round a floating-point number to an exponential scale, such as 0.1, 0.2, 0.4, 0.8, 1.6, 3.2, etc. however I am using an expression language that only has access to arithmetic and functions such as exp(x), log(x), log10(x), sqrt(x), sqr(x), and pow(x,y), so no procedures/scripting.

Is it possible?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Paul
  • 158
  • 11

1 Answers1

1

We start with:

0.1 0.2 0.4 0.8 1.6 etc.

We multiply by 10:

1 2 4 8 16 etc.

We take the log and divide by log 2:

0 1 2 3 4 etc.

We can round to this.

Then we just undo everything.

exp(round(log(val * 10) / log(2)) * log(2)) / 10

Implementation of round() is left for the reader.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358