Decimal vs Double Speed

Floating point arithmetic will almost always be significantly faster because it is supported directly by the hardware. So far almost no widely used hardware supports decimal arithmetic (although this is changing, see comments). Financial applications should always use decimal numbers, the number of horror stories stemming from using floating point in financial applications is endless, … Read more

How to cast from List to double[] in Java?

With java-8, you can do it this way. double[] arr = frameList.stream().mapToDouble(Double::doubleValue).toArray(); //via method reference double[] arr = frameList.stream().mapToDouble(d -> d).toArray(); //identity function, Java unboxes automatically to get the double value What it does is : get the Stream<Double> from the list map each double instance to its primitive value, resulting in a DoubleStream call … Read more

Why floating point value such as 3.14 are considered as double by default in MSVC?

That’s what the C++ (and C) standard decided. Floating point literals are of type double, and if you need them to be floats, you suffix them with a f. There doesn’t appear to be any specifically stated reason as to why, but I’d guess it’s a) For compatibility with C, and b) A trade-off between … Read more

Portability of binary serialization of double/float type in C++

Brian “Beej Jorgensen” Hall gives in his Guide to Network Programming some code to pack float (resp. double) to uint32_t (resp. uint64_t) to be able to safely transmit it over the network between two machine that may not both agree to their representation. It has some limitation, mainly it does not support NaN and infinity. … Read more

Formatting a double to two decimal places

string.Format will not change the original value, but it will return a formatted string. For example: Console.WriteLine(“Earnings this week: {0:0.00}”, answer); Note: Console.WriteLine allows inline string formatting. The above is equivalent to: Console.WriteLine(“Earnings this week: ” + string.Format(“{0:0.00}”, answer));

Limiting double to 3 decimal places

Doubles don’t have decimal places – they’re not based on decimal digits to start with. You could get “the closest double to the current value when truncated to three decimal digits”, but it still wouldn’t be exactly the same. You’d be better off using decimal. Having said that, if it’s only the way that rounding … Read more

DOUBLE vs DECIMAL in MySQL

Actually it’s quite different. DOUBLE causes rounding issues. And if you do something like 0.1 + 0.2 it gives you something like 0.30000000000000004. I personally would not trust financial data that uses floating point math. The impact may be small, but who knows. I would rather have what I know is reliable data than data … Read more

tech