Json.NET serializing float/double with minimal decimal places, i.e. no redundant “.0”?

As an alternative answer to question 2 (assuming you don’t want to go through the hassle of compiling your own custom version of the Json.NET source) you can create your own custom JsonConverter class to handle decimal, float, and double values. Here’s the version I’m using: class DecimalJsonConverter : JsonConverter { public DecimalJsonConverter() { } … Read more

What is the size of float and double in C and C++? [duplicate]

Excerpt from the C99 standard, normative annex F (The C++-standard does not explicitly mention this annex, though it includes all affected functions without change per reference. Also, the types have to match for compatibility.): IEC 60559 floating-point arithmetic F.1 Introduction 1 This annex specifies C language support for the IEC 60559 floating-point standard. The IEC … Read more

Set back default floating point print precision in C++

You can get the precision before you change it, with std::ios_base::precision and then use that to change it back later. You can see this in action with: #include <ios> #include <iostream> #include <iomanip> int main (void) { double pi = 3.141592653590; std::streamsize ss = std::cout.precision(); std::cout << “Initial precision = ” << ss << ‘\n’; … Read more

Is “banker’s rounding” really more numerically stable?

Yes! It really is more numerically stable. For the case that you’re looking at, the numbers [0.0, 0.1, …, 0.9], note that under round-ties-to-away, only four of those numbers are rounding down (0.1 through 0.4), five are rounded up, and one (0.0) is unchanged by the rounding operation, and then of course that pattern repeats … Read more

how set numpy floating point accuracy?

Do you care about the actual precision of the result, or about getting the exact same digits back from your two calculations? If you just want the same digits, you could use np.around() to round the results to some appropriate number of decimal places. However, by doing this you’ll only reduce the precision of the … Read more

How to perform unittest for floating point outputs? – python

The precision of float in Python is dependent on the underlying C representation. From Tutorial/Floating Point Arithmetic: Issues and Limitations, 15.1: Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. As for testing, a better idea is to use existing functionality, e.g. … Read more

How to wrap a float to the interval [-pi, pi)

Modulo function updated to handle boundary cases as noted by aka.nice and arr_sea: static const double _PI= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348; static const double _TWO_PI= 6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696; // Floating-point modulo // The result (the remainder) has same sign as the divisor. // Similar to matlab’s mod(); Not similar to fmod() – Mod(-3,4)= 1 fmod(-3,4)= -3 template<typename T> T Mod(T … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)