significant-digits

How do I round a float to a specified number of significant digits in Ruby?

半世苍凉 提交于 2019-12-01 03:53:06
It would be nice to have an equivalent of R's signif function in Ruby. For example: >> (11.11).signif(1) 10 >> (22.22).signif(2) 22 >> (3.333).signif(2) 3.3 >> (4.4).signif(3) 4.4 # It's usually 4.40 but that's OK. R does not print the trailing 0's # because it returns the float data type. For Ruby we want the same. >> (5.55).signif(2) 5.6 There is probably better way, but this seems to work fine: class Float def signif(signs) Float("%.#{signs}g" % self) end end (1.123).signif(2) # => 1.1 (11.23).signif(2) # => 11.0 (11.23).signif(1) # => 10.0 Some of the previous answers and comments have

How to get excel to display a certain number of significant figures?

泄露秘密 提交于 2019-12-01 02:13:23
问题 I am using excel and i want to display a value to a certain number of significant figures. I tried using the following equation =ROUND(value,sigfigs-1-INT(LOG10(ABS(value)))) with value replaced by the number I am using and sigfigs replaced with the number of significant figures I want. This formula works sometimes, but other times it doesn't. For instance, the value 18.036, will change to 18, which has 2 significant figures. The way around this is to change the source formatting to retain 1

How do I round a float to a specified number of significant digits in Ruby?

淺唱寂寞╮ 提交于 2019-12-01 00:47:48
问题 It would be nice to have an equivalent of R's signif function in Ruby. For example: >> (11.11).signif(1) 10 >> (22.22).signif(2) 22 >> (3.333).signif(2) 3.3 >> (4.4).signif(3) 4.4 # It's usually 4.40 but that's OK. R does not print the trailing 0's # because it returns the float data type. For Ruby we want the same. >> (5.55).signif(2) 5.6 回答1: There is probably better way, but this seems to work fine: class Float def signif(signs) Float("%.#{signs}g" % self) end end (1.123).signif(2) # => 1

Is floating point precision mutable or invariant?

限于喜欢 提交于 2019-11-30 12:40:56
问题 I keep getting mixed answers of whether floating point numbers (i.e. float , double , or long double ) have one and only one value of precision, or have a precision value which can vary. One topic called float vs. double precision seems to imply that floating point precision is an absolute. However, another topic called Difference between float and double says, In general a double has 15 to 16 decimal digits of precision Another source says, Variables of type float typically have a precision

Is there a way to get the “significant figures” of a decimal?

♀尐吖头ヾ 提交于 2019-11-30 08:30:45
Update OK, after some investigation, and thanks in big part to the helpful answers provided by Jon and Hans, this is what I was able to put together. So far I think it seems to work well. I wouldn't bet my life on its total correctness, of course. public static int GetSignificantDigitCount(this decimal value) { /* So, the decimal type is basically represented as a fraction of two * integers: a numerator that can be anything, and a denominator that is * some power of 10. * * For example, the following numbers are represented by * the corresponding fractions: * * VALUE NUMERATOR DENOMINATOR * 1

Is floating point precision mutable or invariant?

喜欢而已 提交于 2019-11-30 02:43:48
I keep getting mixed answers of whether floating point numbers (i.e. float , double , or long double ) have one and only one value of precision, or have a precision value which can vary. One topic called float vs. double precision seems to imply that floating point precision is an absolute. However, another topic called Difference between float and double says, In general a double has 15 to 16 decimal digits of precision Another source says, Variables of type float typically have a precision of about 7 significant digits Variables of type double typically have a precision of about 16

Is there a way to get the “significant figures” of a decimal?

自古美人都是妖i 提交于 2019-11-29 11:30:28
问题 Update OK, after some investigation, and thanks in big part to the helpful answers provided by Jon and Hans, this is what I was able to put together. So far I think it seems to work well. I wouldn't bet my life on its total correctness, of course. public static int GetSignificantDigitCount(this decimal value) { /* So, the decimal type is basically represented as a fraction of two * integers: a numerator that can be anything, and a denominator that is * some power of 10. * * For example, the

How to round down to the nearest significant figure in php

青春壹個敷衍的年華 提交于 2019-11-28 12:30:36
Is there any slick way to round down to the nearest significant figure in php? So: 0->0 9->9 10->10 17->10 77->70 114->100 745->700 1200->1000 ? $numbers = array(1, 9, 14, 53, 112, 725, 1001, 1200); foreach($numbers as $number) { printf('%d => %d' , $number , $number - $number % pow(10, floor(log10($number))) ); echo "\n"; } Unfortunately this fails horribly when $number is 0, but it does produce the expected result for positive integers. And it is a math-only solution. Here's a pure math solution. This is also a more flexible solution if you ever wanted to round up or down, and not just down.

Nicely representing a floating-point number in python

安稳与你 提交于 2019-11-28 09:13:39
I want to represent a floating-point number as a string rounded to some number of significant digits, and never using the exponential format. Essentially, I want to display any floating-point number and make sure it “looks nice”. There are several parts to this problem: I need to be able to specify the number of significant digits. The number of significant digits needs to be variable, which can't be done with with the string formatting operator . [edit] I've been corrected; the string formatting operator can do this. I need it to be rounded the way a person would expect, not something like 1

Preventing double.Parse from removing trailing zeros after decimal place?

孤人 提交于 2019-11-28 07:48:46
问题 When using double.Parse, it seems to like to string away any trailing (insignificant) zeros from the string that I'm converting. I would like double.Parse to keep to places after the decimal. For example, here is some code: tobereturned.MouseSensitivty = double.Parse(String.Format("{0:#.##}", tempstring[1])); Debug.WriteLine("Converted " + String.Format("{0:#.##}", tempstring[1]) + " to " + tobereturned.MouseSensitivty); The Debugger then writes Converted 4.00 to 4 So it seems like double