Issue: I'm having an issue with the decimal/floating point math in JavaScript/Typescript. , but I'm having no luck. Desired Result: 82.8 Result: 82.80000000000001 What's Working: total += (array[i] * weight[i] * 100) / 100; line is correctly working, but that same logic isn't working with the total. What I've Tried:  In another function, I've multiplied and divided the value by 10 or 100. This works, but I can't get it to work in my case.  const remainder: number = ((1 - Math.sqrt(value)) * 10) / 10;   People have suggested using toFixed(2);, but that changes the typecast of the number to a string, which is what I don't want.  calulateWeightedAverage.tsx: // Calculate Weighted Average export const calculateAverageWeighted = (array: Array&lt;number&gt;, weight: Array&lt;number&gt;) =&gt; {   // Check If Data Exists   if (array.length &gt;= 1) {     // Weight Length: Must Equal Array Length     if (weight.length == array.length) {       // Weight Sum: Must Equal 1 Or 100       if (weight.reduce((total: number, value: number) =&gt; { return total + value }) === 1 || weight.reduce((total: number, value: number) =&gt; { return total + value }) === 100) {         // Total         let total: number = 0;          // Iterate Over Array         let i: number = 0;         while (i &lt; array.length) {           // Add To Total           total += (array[i] * weight[i] * 100) / 100;            // Increase I           i++;         }          // IMPORTANT: FLOAT MATH (NOT WORKING HERE)         return (total * 100) / 100;       }       else {         // Error         throw new Error('Error: Values of Weight Array must be equal to 1 or 100 (calculateAverageWeighted)');       }     }     // Array Length Is Less Than Weight Length     else if (array.length &lt; weight.length || array.length &gt; weight.length) {       // Error       throw new Error('Error: Length of weight must be equal to input Array (calculateAverageWeighted)');     }   }   else {     // Error     throw new Error('Error: Empty Array (calculateAverageWeighted)');   } };
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?
44106
Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions.  Obviously, I can do something like:  double clampedA; double a = calculate(); clampedA = a &gt; MY_MAX ? MY_MAX : a; clampedA = a &lt; MY_MIN ? MY_MIN : a;   or  double a = calculate(); double clampedA = a; if(clampedA &gt; MY_MAX)     clampedA = MY_MAX; else if(clampedA &lt; MY_MIN)     clampedA = MY_MIN;   The fixpoint version would use functions/macros for comparisons.  This is done in a performance-critical part of the code, so I'm looking for an as efficient way to do it as possible (which I suspect would involve bit-manipulation)   EDIT: It has to be standard/portable C, platform-specific functionality is not of any interest here. Also, MY_MIN and MY_MAX are the same type as the value I want clamped (doubles in the examples above).
47848
I want to align two numbers in two different rows the decimal point.    I do see a link for that here:   However, I don't understand how to incorporate that into my code.  What follows is a reduced version of my code.  \documentclass[12pt,english]{article} \usepackage{longtable} \usepackage{fullpage} \usepackage{times} \usepackage[flushleft]{threeparttable} \usepackage[font=large,labelfont=bf,tableposition=top,textfont=bf]{caption} \usepackage{tabularx} \usepackage{booktabs} \newcolumntype{C}{&gt;{\centering\arraybackslash}X}  \begin{document}  \clearpage \newpage \begin{table}[!ht] \caption{Table Title} \def\arraystretch{1.05} \vspace{-0.2cm} \begin{threeparttable} \small \begin{tabularx}{\textwidth}{l*{10}{C}} \hline \hline \addlinespace &amp; (1)  \\   Variable Name &amp; 0.1234566  \\ &amp; (0.1234566) \\ \hline \hline \addlinespace  \end{tabularx} \begin{tablenotes} \vspace{0.1cm} \footnotesize{  \item \noindent \hspace{-1.8mm} Notes:    \noindent Sources:   } \end{tablenotes} \end{threeparttable} \end{table}  \end{document}
6894
How do I calculate relative error when the true value is zero?  Say I have $x_{true} = 0$ and $x_{test}$. If I define relative error as:  $\text{relative error} = \frac{x_{true}-x_{test}}{x_{true}}$  Then the relative error is always undefined. If instead I use the definition:  $\text{relative error} = \frac{x_{true}-x_{test}}{x_{test}}$  Then the relative error is always 100%. Both methods seem useless. Is there another alternative?
113353
I'm working with large numbers that I can't have rounded off.  Using Lua's standard math library, there seem to be no convenient way to preserve precision past some internal limit.  I also see there are several libraries that can be loaded to work with big numbers:      (might be identical to #2)  (but I can't find any source)   Further, there are  in C that could be called from Lua, if the bindings where established.  Have you had any experience with one or more of these libraries?
108828
I have one calculated column and I want to display the sum of all its values but I know that's impossible in a OOTB way. How can I copy all the values to a new column (type number)?  Do I need to use JavaScript?   ps: I'm using SharePoint on-line
19825
But here's an example:  Dim desiredType as Type if IsNumeric(desiredType) then ...   EDIT: I only know the Type, not the Value as a string.  Ok, so unfortunately I have to cycle through the TypeCode.  But this is a nice way to do it:   if ((desiredType.IsArray))       return 0;   switch (Type.GetTypeCode(desiredType))  {       case 3:       case 6:       case 7:       case 9:       case 11:       case 13:       case 14:       case 15:           return 1;  }  ;return 0;
40138
What's the proper way to convert from a scientific notation string such as "1.234567E-06" to a floating point variable using C#?
98548
What is wrong with this code?  = IF (F3+E3+D3 &gt; 0, F3+E3+D3, 0)   I want the cell to contain the sum only if it is positive. If it is negative I want zero to be there.  Google Spreadsheet gives me error: Parse error.
40928
How can you check whether a string is convertible to an int?  Let's say we have data like "House", "50", "Dog", "45.99", I want to know whether I should just use the string or use the parsed int value instead.  In JavaScript we had this  function. If the string couldn't be parsed, it would get back NaN.
49746
The  (chapter 6.5, set function specification) the following behaviour for aggregate functions on empty result sets:  COUNT(...) = 0 AVG(...) = NULL MIN(...) = NULL MAX(...) = NULL SUM(...) = NULL   Returning NULL for AVG, MIN and MAX makes perfect sense, since the average, minimum and maximum of an empty set is undefined.  The last one, however, bothers me: Mathematically, the SUM of an empty set is well-defined: 0. Using 0, the  of addition, as the base case makes everything consistent:  SUM({})        = 0    = 0 SUM({5})       = 5    = 0 + 5 SUM({5, 3})    = 8    = 0 + 5 + 3 SUM({5, NULL}) = NULL = 0 + 5 + NULL   Defining SUM({}) as null basically makes "no rows" a special case that does not fit in with the others:  SUM({})     = NULL  = NULL SUM({5})    = 5    != NULL + 5 (= NULL) SUM({5, 3}) = 8    != NULL + 5 + 3 (= NULL)   Is there some obvious advantage of the choice that was made (SUM being NULL) that I have missed?
67645
I know that a float isn't appropriate to store currency values because of rounding errors. Is there a standard way to represent money in C++?   I've looked in the boost library and found nothing about it. In java, it seems that BigInteger is the way but I couldn't find an equivalent in C++. I could write my own money class, but prefer not to do so if there is something tested.
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.
58941
I want to use a Track-Bar to change a Form's opacity. This is my code: decimal trans = trackBar1.Value / 5000; this.Opacity = trans;  When I build the application, it gives the following error:  Cannot implicitly convert type decimal to double   I have tried using trans and double, but then the Control doesn't work. This code worked fine in a past VB.NET project.
11789
I'd like to convert a float to a whole number in JavaScript. Actually, I'd like to know how to do BOTH of the standard conversions: by truncating and by rounding. And efficiently, not via converting to a string and parsing.
5995
When displaying the value of a decimal currently with .ToString(), it's accurate to like 15 decimal places, and since I'm using it to represent dollars and cents, I only want the output to be 2 decimal places.   Do I use a variation of .ToString() for this?
105321
I have a bit of a unique problem. Well, maybe not a problem because I'm really just curious about it, but...  I have a simple 8 digit calculator. It has +, -, x, /, and a constant operation function. It has no usable memory slot.  I need (lit. would like to know how) to calculate ln(x) on this calculator. Any series I have found lose a lot of precision since whatever is outside the 8 digit range is truncated without rounding. I have used Padé approximants with trig functions, and a continued fraction for exp(), but none of these two methods are adequate for ln(x), either because there is no way around the truncation error or it requires writing down many intermediate values, or it requires memorizing and punching in many constants.  Does anyone know any good way to calculate ln(x) (or any other base, since memorizing one constant is OK) to as many digits as possible as easily as possible on this calculator (bonus points to methods where the value on the screen is immediately, meaning you don't need to write it down)?  For that matter, if you happen to know some good ways to calculate other special functions, I am interested in that as well.  Thanks!