using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3  I have tried different typecasting of scaledvalue2 but not until I stored the multiplication in a double variable and then to an int could I get desired result.. but I can't explain why ???  I know double precission(0.6999999999999999555910790149937383830547332763671875) is an issue but I don't understand why one way is OK and the other is not ??  I would expect both to fail if precision is a problem.  I DON'T NEED solution to fix it.. but just a WHY ?? (the problem IS fixed)  void main() {     double value = 0.7;     int scaleFactor = 1000;      double doubleScaled = (double)scaleFactor * value;      int scaledvalue1 = doubleScaled; // = 700      int scaledvalue2 = (double)((double)(scaleFactor) * value);  // = 699 ??      int scaledvalue3 = (double)(1000.0 * 0.7);  // = 700       std::ostringstream oss;     oss &lt;&lt; scaledvalue2;     printf("convert FloatValue[%f] multi with %i to get %f = %i or %i or %i[%s]\r\n",       value,scaleFactor,doubleScaled,scaledvalue1,scaledvalue2,scaledvalue3,oss.str().c_str());  }   or in short:  value = 0.6999999999999999555910790149937383830547332763671875; int scaledvalue_a = (double)(1000 * value);  // =  699?? int scaledvalue_b = (double)(1000 * 0.6999999999999999555910790149937383830547332763671875);  // =  700 // scaledvalue_a = 699 // scaledvalue_b = 700   I can't figure out what is going wrong here.  Output :  convert FloatValue[0.700000] multi with 1000 to get 700.000000 = 700 or 699 or 700[699]      vendor_id       : GenuineIntel      cpu family      : 6      model           : 54      model name      : Intel(R) Atom(TM) CPU N2600   @ 1.60GHz
27
Consider the following code:   0.1 + 0.2 == 0.3  -&gt;  false   0.1 + 0.2         -&gt;  0.30000000000000004   Why do these inaccuracies happen?
115153
I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case.  While I can understand imprecisions between 0.00000000000001 and 0.00000000000002, 0 itself seems pretty hard to mess up since it's just nothing.  If you're imprecise on nothing, it's not nothing anymore.  But I don't know much about this topic so it's not for me to say.  double x = 0.0; return (x == 0.0) ? true : false;   Will that always return true?
73261
The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function?
118714
When dealing with double data types is multiplying by the inverse better or worse? Which way is faster? Which way uses less memory? Which way is preferred? How does MSIL handle this? SquareInches = MMSquared / 645.16  SquareInches = MMSquared * 0.0015500031000062000124000248000496    NB: 10K users will note that this is a duplicate of , which was deleted because the original question asker decided to berate everyone in the 'comments' section of the question.  This question was reposted because it is a 'good' question. Please 'uncheck' Community Wiki for your answer, as I'm only posting this as CW so that it's not seen as a 'reputation' grab. Related Question:
7653
public class doublePrecision {     public static void main(String[] args) {          double total = 0;         total += 5.6;         total += 5.8;         System.out.println(total);     } }   The above code prints:  11.399999999999   How would I get this to just print (or be able to use it as) 11.4?
36557
I am curious as to why an implicit cast fails in...  int? someValue = SomeCondition ? ResultOfSomeCalc() : null;   and why I have to perform an explicit cast instead  int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null;   It seems to me that the compiler has all the information it need to make an implicit casting decision, no?
17772
Very basic question: how do I write a short literal in C++?  I know the following:   2 is an int 2U is an unsigned int 2L is a long 2LL is a long long 2.0f is a float 2.0 is a double '\2' is a char.   But how would I write a short literal?  I tried 2S but that gives a compiler warning.
78703
If have this in the setter of a property:  decimal? temp = value as decimal?;   value = "90"  But after the cast, temp is null...  What is the proper way to do this cast?
95377
Firstly I do not want to round my numbers, I am using base 10 figures so excel should have no bother doing simple add and subtract.  A|  665.110000000000000000000000000000 B|  4.990000000000000000000000000000 C|  =a1+b1 D|  670.790000000000000000000000000000 E|  1,335.890000000000000000000000000000 F|  =e1-(c1+d1)   I should get -5 however F1  returns   -4.999999999999770000000000000000   It is a financial document therefore I want to avoid rounding, more to the point. Why should I need to? I've put cell formatting to Currency with 2 decimal placed and I will get the correct display value but for all calculations I will still get -4.9999...7 which leads to further errors with conditional formatting etc.
96922
Why when I save a value of say 40.54 in SQL Server to a column of type Real does it return to me a value that is more like 40.53999878999 instead of 40.54?  I've seen this a few times but have never figured out quite why it happens.  Has anyone else experienced this issue and if so causes it?
117193
Do we need to be careful when comparing a double value against zero?  if ( someAmount &lt;= 0){ ..... }
99240
I write financial applications where I constantly battle the decision to use a double vs using a decimal.  All of my math works on numbers with no more than 5 decimal places and are not larger than ~100,000. I have a feeling that all of these can be represented as doubles anyways without rounding error, but have never been sure.  I would go ahead and make the switch from decimals to doubles for the obvious speed advantage, except that at the end of the day, I still use the ToString method to transmit prices to exchanges, and need to make sure it always outputs the number I expect. (89.99 instead of 89.99000000001)  Questions:   Is the speed advantage really as large as naive tests suggest? (~100 times) Is there a way to guarantee the output from ToString to be what I want? Is this assured by the fact that my number is always representable?   UPDATE: I have to process ~ 10 billion price updates before my app can run, and I have implemented with decimal right now for the obvious protective reasons, but it takes ~3 hours just to turn on, doubles would dramatically reduce my turn on time. Is there a safe way to do it with doubles?
102992
I  have come up with the following expression to convert 5.1234   to   5° 7`24.24"   toint ($x) || '° ' || toint((($x) - toint ($x)) * 60) ||'`' || substr( (tostring(((($x) - toint ($x)) * 60) - toint((($x) - toint ($x)) * 60)) * 60),1,5) || '"'  Here’s the problem : for some  points such as 5.1234, it works. but for others,  it doesn’t.  I suspect the problem is the integer conversion which round up the decimal figures instead of truncating it.  Is there any other option? thanks.
18919
I am typing to get a sale amount (by input) to be multiplied by a defined sales tax (0.08) and then have it print the total amount (sales tax times sale amount).  I run into this error. Anyone know what could be wrong or have any suggestions?   salesAmount = raw_input (["Insert sale amount here \n"]) ['Insert sale amount here \n']20.99 &gt;&gt;&gt; salesTax = 0.08 &gt;&gt;&gt; totalAmount = salesAmount * salesTax  Traceback (most recent call last):   File "&lt;pyshell#57&gt;", line 1, in &lt;module&gt;     totalAmount = salesAmount * salesTax TypeError: can't multiply sequence by non-int of type 'float'
114888
I'm in the process of creating a class that stores metadata about a particular data source.  The metadata is structured in a tree, very similar to how XML is structured.  The metadata values can be integer, decimal, or string values.  I'm curious if there is a good way in C++ to store variant data for a situation like this.  I'd like for the variant to use standard libraries, so I'm avoiding the COM, Ole, and SQL VARIANT types that are available.  My current solution looks something like this:  enum MetaValueType {     MetaChar,     MetaString,     MetaShort,     MetaInt,     MetaFloat,     MetaDouble };  union MetaUnion {     char cValue;     short sValue;     int iValue;     float fValue;     double dValue; };  class MetaValue { ... private:     MetaValueType ValueType;     std::string StringValue;     MetaUnion VariantValue; };   The MetaValue class has various Get functions for obtaining the currently stored variant value, but it ends up making every query for a value a big block of if/else if statements to figure out which value I'm looking for.  I've also explored storing the value as only a string, and performing conversions to get different variant types out, but as far as I've seen this leads to a bunch of internal string parsing and error handling which isn't pretty, opens up a big old can of precision and data loss issues with floating point values, and still doesn't eliminate the query if/else if issue stated above.  Has anybody implemented or seen something that's cleaner to use for a C++ variant data type using standard libraries?
70173
While working on a project, I came across a JS-script created by a former employee that basically creates a report in the form of  Name : Value Name2 : Value2   etc.  The peoblem is that the values can sometimes be floats (with different precision), integers, or even in the form 2.20011E+17. What I want to output are pure integers. I don't know a lot of JavaScript, though.  How would I go about writing a method that takes these sometimes-floats and makes them integers?
38145
I know, the question seems to be strange. Programmers sometimes think too much. Please read on... In C I use signed and unsigned integers a lot. I like the fact that the compiler warns me if I do things like assigning a signed integer to an unsigned variable. I get warnings if I compare signed with unsigned integers and much much more. I like these warnings. They help me to keep my code correct. Why don't we have the same luxury for floats? A square-root will definitely never return a negative number. There are other places as well where a negative float value has no meaning. Perfect candidate for an unsigned float. Btw - I'm not really keen about the single extra bit of precision that I could get by removing the sign bit from the floats. I'm super happy with floats as they are right now.  I'd just like to mark a float as unsigned sometimes and get the same kind of warnings that I get with integers. I'm not aware of any programming language that supports unsigned floating-point numbers. Any idea why they don't exist?  EDIT: I know that the x87 FPU has no instructions to deal with unsigned floats. Lets just use the signed float instructions. Misuse (e.g. going below zero) could be considered undefined behaviour in the same way as overflow of signed integers is undefined.