double

Round off a double while maintaining the trailing zero

血红的双手。 提交于 2021-02-08 12:54:07
问题 Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5 public static double roundOff(double number) { double accuracy = 20; number = number * accuracy; number = Math.ceil(number); number = number / accuracy; return number; } So if I send 1.499 it returns 1.5 where as I want 1.50 回答1: This is a printing poblem: double d = 1.5; System.out.println(String.format("%.2f", d)); // 1.50 回答2: 1.5 is,

Does calloc() of a double field always evaluate to 0.0?

寵の児 提交于 2021-02-07 14:12:39
问题 Does calloc() of a double field always evaluate to 0.0 ? Furthermore : Does calloc() of a float field always evaluate to 0.0f ? Does calloc() of an int or unsigned int field always evaluate to 0 ? That is , will the assert() below always succeed on all platforms? double* d = calloc(1, sizeof(double)); assert(*d == 0.0); free(d); 回答1: The calloc sets all bytes of the allocated memory to zero. As it happens, that's also the valid IEEE754 (which is the most common format for floating point

Good sentinel value for double if prefer to use -ffast-math

前提是你 提交于 2021-02-07 11:58:38
问题 Since the gcc option -ffast-math effectively disables NaN and -/+inf , I'm looking for maybe the next best option for representing NaN in my performance-critical math code. Ideally the sentinel value if operated on (add, mul, div, sub, etc..) would yield the sentinel value as NaN would do but I doubt this would be possible since I think NaN is the only value that accomplishes this. -0.0 might not be a good fit as it's also disabled in -ffast-math and could prevent certain optimizations like

read double type data from binary file

拥有回忆 提交于 2021-02-06 13:47:46
问题 I want to read double values from a binary file and store them in a vector. My values have the following form: 73.6634, 73.3295, 72.6764 and so on. I have this code that read and store data in memory. It works perfectly with char types since the read function has as input a char type ( istream& read (char* s, streamsize n) ). When I try to convert char type to double I get obviously integer values as 74, 73, 73 and so on. Is there any function which allows me to read directly double values or

convert Matrix of type CV_32FC1 to CV_64FC1

三世轮回 提交于 2021-02-05 14:36:53
问题 How do I convert a cv::Mat of type CV_32FC1 to the type CV_64FC1 (equivalent to a change from float to double)? I am opening a Matrix that was saved as XML ( cvSave ) but as a float. This means that the field <dt> has the value f in the file. I need to change it to d to open it. But I'd rather not do this, instead I'd like to open it directly as a Matrix with elements of type double, or convert it later from float to double. Below is my code for opening the file. /** Load cv::Mat from XML

Why are double and long double completely the same on my 64 bit machine?

房东的猫 提交于 2021-02-04 15:01:29
问题 This question may sound like for beginners, however when I found that out I thought I'm either a beginner or my comp is missing something: int main() { cout << sizeof(double) << endl; cout << sizeof(long double) << endl; cout << DBL_DIG << endl; cout << LDBL_DIG << endl; return 0; } PROGRAM OUTPUT: 8 8 15 15 I thought long double is 10 bytes and has 18 decimal digits while double is 8 bytes and has 15 digits but it seems I was wrong. Why is that so? Using MSVC 2010 on 64bit machine. 回答1: In

Why are double and long double completely the same on my 64 bit machine?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-04 15:00:52
问题 This question may sound like for beginners, however when I found that out I thought I'm either a beginner or my comp is missing something: int main() { cout << sizeof(double) << endl; cout << sizeof(long double) << endl; cout << DBL_DIG << endl; cout << LDBL_DIG << endl; return 0; } PROGRAM OUTPUT: 8 8 15 15 I thought long double is 10 bytes and has 18 decimal digits while double is 8 bytes and has 15 digits but it seems I was wrong. Why is that so? Using MSVC 2010 on 64bit machine. 回答1: In

Drop un-needed decimal “.0”

心不动则不痛 提交于 2021-02-02 09:54:25
问题 I'm making a simple calculator and I am having an issue with it showing a decimal on what I want to just be a whole number. For example, if the entered expression is "50 + 50" the answer will come out "100.0". I understand that it's happening because my output is set as a double, but I can't figure out how to convert those numbers to integer only when the answer is ".0". My output answer code: fun equal (view: View) { secondnum = editText.text.toString() decpressed = 0 var sum = 0.0 when (op)

What does -1.#IND mean (double stream output) [duplicate]

半城伤御伤魂 提交于 2021-01-29 07:59:18
问题 This question already has answers here : Why are the return values of these doubles -1.#IND? (3 answers) Closed 6 years ago . I could neither find it via google, search here or on Microsofts helppages... After some extensive calculations, sometimes, when outputting my doubles via std::cout i prints as result on console: -1.#IND There are no modifcations(like precision etc) to the cout-stream. I assume the program wants to tell me about some sort of error, but I can't figure it out :/ It doesn

Is there built-in way to check if string can be converted to float?

我的梦境 提交于 2021-01-29 07:26:13
问题 I know there are ways to check if str can be converted using try-except or regular expressions, but I was looking for (for example) str methods such as str.isnumeric() str.isdigit() str.isdecimal() # also doesn't work for floats but could't find any. Are there any I haven't found yet? 回答1: As stated by and Macattack and Jab, you could use try except , which you can read about in python's docs or w3school's tutorial. Try Except clauses have the form: try: # write your code pass except