double

How to round a double value to a selected number of decimals in .NET?

自闭症网瘾萝莉.ら 提交于 2019-12-24 07:35:24
问题 I use an acceleration sensor to calculate the current accelerations and it returns the double value. However I would like to compare the current acceleration with value 9.8. Before doing that i have to round the value received from the sensor so the question is: How to round a double value to a selected number of decimals in .NET? 回答1: Math.Round(number, precision) http://msdn.microsoft.com/en-us/library/zy06z30k.aspx 回答2: Math.Round - i.e. double val = Math.Round(current, 1); // 1dp 来源:

Double to Byte array conversion in javascript

こ雲淡風輕ζ 提交于 2019-12-24 07:25:36
问题 I am trying to convert some Java code into JavaScript which is needed for the application I am working on. I am stuck on one class and its methods that convert a variable of type double to long, and then long to a byte array that consists of 8 bytes representing that long number. The Java code is as follows: public static byte[] doubleToByteArray(double number) { // double to long representation long longNum = Double.doubleToLongBits(number); // long to 8 bytes return new byte[] {(byte)(

double comparison to zero special case?

被刻印的时光 ゝ 提交于 2019-12-24 04:18:10
问题 I'm initializing a double array: double [] foo = new double[n]; My understanding is that the java language specification causes all the values in the array to be initialized to zero. As I go through my algorithm, some of the entries in the array get set to a positive value. So to check whether a particular element has a non-zero value set, is it safe to just use if (foo[i] > 0.0) or should I be using an epsilon there. Or similarly if I want to know if a value has not been set, could I use ==

Is there any benefit of not using double on a 64bit (and using, say, float instead) processor?

百般思念 提交于 2019-12-24 03:29:15
问题 I always use double to do calculations but double offers far better accuracy than I need (or makes sense, considering that most of the calculations I do are approximations to begin with). But since the processor is already 64bit, I do not expect that using a type with less bits will be of any benefit. Am I right/wrong, how would I optimize for speed (I understand that smaller types would be more memory efficient) here is the test #include <cmath> #include <ctime> #include <cstdio> template

Where does C++ standard define the value range of float types?

核能气质少年 提交于 2019-12-24 01:45:24
问题 As far as I know floating point values are of the form n * 2^e, with float range being n = -(2^23-1) - (2^23-1), and e = -126 - 127, double range being n = -(2^52-1) - (2^52-1), and e = -1022 - 1023 I was looking through the C++ standard, but failed to find the place where the standard specifies this, or mandates the association of the float, double and long double types with ranges defined in other (IEEE) standards. The only related thing I found in 3.9.1.8 is: There are three floating point

Avoiding float to pointer coercion in Common Lisp

好久不见. 提交于 2019-12-24 01:13:29
问题 I use SBCL (64-bit v1.4.0) for numerical calculation. After enabling optimization, following compiler note appears: note: doing float to pointer coercion (cost 13) to "<return value>" The code I use is as follows: (defun add (a b) (declare (optimize (speed 3) (safety 0))) (declare (double-float a b)) (the double-float (+ a b))) I've also tried ftype and got the same note. On the other hand, following code doesn't show the note: (defun add-fixnum (a b) (declare (optimize (speed 3) (safety 0)))

what is -0.0000 in c when using floats and double?

喜你入骨 提交于 2019-12-24 00:38:35
问题 #include <stdio.h> #include <stdlib.h> #define answer 3.141593 void main(int argc, char **argv) { float a = (argc - 2)?: strtod(argv[1], 0); printf("double = %lf ,float = %f", a-answer , a-answer); } when I run it like that: ./a.out 3.141593 the output is double = -0.000000 ,float = -0.000000 Why does it -0.00000 ? how can I make it output 0.000000 ? How can I make a == answer ? How comes there is -0 value if it uses 2's complement? 回答1: Floating point numbers doesn't use 2's complement. They

Swift 3 Migration - Double Extension rounding issue

不打扰是莪最后的温柔 提交于 2019-12-23 22:12:09
问题 I'm migrating our codebase to Swift 3 and I've come across a compilation issue that I can't explain or fix. I have a method in a Double extension that rounds the Double to a certain number of digits: public func roundToPlaces(places: Int) -> Double { let divisor = pow(10.0, Double(places)) return round(self * divisor) / divisor } For example: 12.34567.roundToPlaces(2) should return 12.35 . However, I'm getting a compilation issue for the round method used in this extension. It's saying that I

Android: Handle backspace on InputFilter

与世无争的帅哥 提交于 2019-12-23 21:53:22
问题 I created an InputFilter for an EditText component that only allows doubles within a range (e.g. from 1.5 to 5.5). Everything worked fine until I deleted the decimal point: I typed 1.68 and then deleted the decimal point. The value in the text field became 168, which is obviously outside the range. Here is a simplified version of my filter public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (isValid(dest.toString() + source.toString()))

problem with Double and Rational Number

巧了我就是萌 提交于 2019-12-23 21:44:31
问题 I am writing a function in which I need to read a string contains floating point number and turn it back to Rational. But When I do toRational (read input :: Double) , it will not turn for eg: 0.9 into 9 % 10 as expected, but instead 81..... % 9007... Thx 回答1: This is correct behavior. The number 0.9 is not representable as a Double , not in Haskell, C, or Java. This is because Double and Float use base 2: they can only represent a certain subset of the dyadic fractions exactly. To get the