double

c#: sum of two double numbers problem [duplicate]

一曲冷凌霜 提交于 2019-12-23 07:58:04
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why is floating point arithmetic in C# imprecise? Hi. I've got following problem: 43.65+61.11=104.75999999999999 for decimal is correct: (decimal)43.65+(decimal)61.11=104.76 Why result for double is wrong? 回答1: This question and its answers are a wealth of info on this - Difference between decimal, float and double in .NET? To quote: For values which are "naturally exact decimals" it's good to use decimal. This

Different rounding with println and printf

回眸只為那壹抹淺笑 提交于 2019-12-23 07:26:37
问题 The first line below will print 0.8999999999999999 because of precision loss, this is clear. But the second line will print 0.9 , I just do not understand why. Shouldn't there be the same problem with this calculation? System.out.println(2.00-1.10); System.out.printf("%f",2.00-1.10); 回答1: I think you are missing something as using System.out.printf(), if you do not explicit formatting widths then default behavior of printf in C (which is 6 decimal places if not explicitly specified) So if you

Is a float guaranteed to be preserved when transported through a double in C/C++?

会有一股神秘感。 提交于 2019-12-23 06:44:52
问题 Assuming IEEE-754 conformance, is a float guaranteed to be preserved when transported through a double? In other words, will the following assert always be satisfied? int main() { float f = some_random_float(); assert(f == (float)(double)f); } Assume that f could acquire any of the special values defined by IEEE, such as NaN and Infinity. According to IEEE, is there a case where the assert will be satisfied, but the exact bit-level representation is not preserved after the transportation

Is double 0.0 is greater or less than 0 in java

独自空忆成欢 提交于 2019-12-23 06:21:51
问题 I would like to ask for more clarification. Here my sample program double diff = 7.500 - 7.500; System.out.println(diff); // result 0.0 if (diff > 0) { System.out.println("+" + diff ); //result +0.0 } else { System.out.println("-" + diff ); //result -0.0 } My result is -0.0 . My expectation is 0 == 0.0 then skip if else condition. But it enter to the else condition.Is double 0.0 is greater or less than 0? 回答1: this is my answer: double diff = 7.500 - 7.500; System.out.println(diff); if(diff>0

Why does multiplying two large double numbers give a wrong result?

送分小仙女□ 提交于 2019-12-23 06:12:43
问题 The output for this double multiplication (double)1000000007 * (double)11111111 should end with 7 (or equal to 11111111077777777 be precise). But this piece of code I wrote outputs the result ending with 6 (or equal to 11111111077777776 to be precise). I cannot figure out what I might be doing wrong. Any help would be great. #include <iostream> #include <iomanip> using namespace std; int main() { cout << setprecision(40) << (double)1000000007 * (double)11111111; } 回答1: When you do

Unable to change NSNumber to Double and do some calculations with it

女生的网名这么多〃 提交于 2019-12-23 05:55:09
问题 my problem is that i have an Array that holds some double values NSArray *level4results = [context executeFetchRequest:request error:&error]; then i sum up all the values in that array NSNumber *l4sum = [level4results valueForKeyPath:@"sum.self"]; The next thing i want to do is to divide by 8 the sum of the array... and this is where i am stuck. I have tried many options and ways of doing it by either way i kept on getting different error. This is what i have currently in my code double

decimal number and exact representation

a 夏天 提交于 2019-12-23 03:32:07
问题 In c++, how to know when a decimal number can be exactly representing using IEEE 754-1985 standard. for instance 0.2 cannot be representing exactly. Is there a simple rule ? 回答1: A number can be represented exactly as an IEEE755 float if it can be written as B × 2 n , where B is an integer (and B and n fall into some valid range). In other words, there must be some integer n such that if you mutliply your number by 2 n you get an integer. Clearly for 1/5 there is no such n . Yet another way

decimal number and exact representation

故事扮演 提交于 2019-12-23 03:32:02
问题 In c++, how to know when a decimal number can be exactly representing using IEEE 754-1985 standard. for instance 0.2 cannot be representing exactly. Is there a simple rule ? 回答1: A number can be represented exactly as an IEEE755 float if it can be written as B × 2 n , where B is an integer (and B and n fall into some valid range). In other words, there must be some integer n such that if you mutliply your number by 2 n you get an integer. Clearly for 1/5 there is no such n . Yet another way

How to use printf with mpfr and mpreal

左心房为你撑大大i 提交于 2019-12-23 01:05:08
问题 What is the correct syntax for using printf and its cousins sprintf and fprintf to display the value of mpreal -type variables? I have tried the naive casting to double: printf ("... %g ...", (double) var); only to receive this error message from g++: error: invalid cast from type ‘mpfr::mpreal’ to type ‘double’ I had no problem using double -type variables elsewhere on the program. I heard about the type mpreal as part of this library intended to enable the use of the usual binary operators

How to use printf with mpfr and mpreal

怎甘沉沦 提交于 2019-12-23 01:04:57
问题 What is the correct syntax for using printf and its cousins sprintf and fprintf to display the value of mpreal -type variables? I have tried the naive casting to double: printf ("... %g ...", (double) var); only to receive this error message from g++: error: invalid cast from type ‘mpfr::mpreal’ to type ‘double’ I had no problem using double -type variables elsewhere on the program. I heard about the type mpreal as part of this library intended to enable the use of the usual binary operators