ruby - Calculate number of significant digits -




i need convert floating point number 5-significant digits format. means 5 first non-zero digits (with last digit not rounded up).

example

float("%.#{5}g" % 0.007425742694) => 0.0074257 float("%.#{5}g" % 3852.574257425742694) => 3852.6 

the problem approach me 3852.574257425742694 rounded 3852.6, , need 3852.5.

i understand numbers non-negative. 1 can following:

code

def floor_it(f, sig_digits=5)   pow = sprintf("%e", f)[-3..-1].to_i   (f *= 10**(-pow)).floor(sig_digits-1) * 10**(pow) end 

examples

floor_it 0.007425742694        #=> 0.0074257 floor_it 3852.574257425742694  #=> 3852.5 

explanation

for

f = 385.74957425742694  sig_digits = 5 

the steps follows. first express number in scientific notation (as string). see kernel#sprintf.

a = sprintf("%e", f)   #=> "3.857496e+02" 

we wish extract last 2 digits.

b = a[-3..-1]   #=> "02" 

convert integer.

pow = b.to_i   #=> 2 

shift f's decimal point there 1 non-zero digit left of decimal point.

f *= 10**(-pow)    #=> 3.8574957425742697 

use method float#floor obtain desired digits.

d = f.floor(sig_digits-1)   #=> 3.8574957425742697.floor(4)   #=> 3.8574 

lastly, shift decimal point initial position.

d * 10**(pow)   #=> 385.74 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -