# All Questions

Tagged with floating-point c++

162
questions with no upvoted or accepted answers

5votes

0answers

37views

### How to parse floating point infinity from std::istream

I have written a superdumb serialization library for a project that I am working on.
I just got bitten by floating point infinity, which I illustrate with the sample program below. I expect the ...

5votes

0answers

116views

### Probable bug in MSVC with compile-time NaN comparison

My colleague was doing some basic experiments with NaN and was puzzled by the behavior on Visual Studio that did not match his expectations. After discussion, it seems that he uncovered a probable ...

5votes

0answers

146views

### How to catch floating point errors early (right at where they occur)?

When developing floating-point heavy code, it is very useful to enable FPU exceptions. When an operation results in a NaN/inf, we could catch it immediately.
For example, on Linux, I can enable this ...

4votes

0answers

384views

### C# Change FPU rounding mode

I'm attempting to write an interval arithmetic library in C# .NET, but in order to do this accurately I need to be able to control the rounding mode of floating point operations.
After a bit of ...

4votes

0answers

2kviews

### pragma STDC FENV_ACCESS ON is not supported

I tried to slightly modify the example from the article:
#include <iostream>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
int main()
{
std::feclearexcept(FE_ALL_EXCEPT);
//int r ...

4votes

2answers

1kviews

### How to correctly pass a float from C# to C++ (dll)

I'm getting huge differences when I pass a float from C# to C++.
I'm passing a dynamic float wich changes over time.
With a debugger I get this:
c++ lonVel -0.036019072 float
c# lonVel -0....

3votes

2answers

544views

### Find smallest integer that satisfies floating point inequality equation

I am looking for a fast algorithm that finds the smallest integer N that will satisfy the following inequality where s, q, u, and p are float numbers (using the IEEE-754 binary32 format):
s > q + ...

3votes

1answer

204views

### Casting float to int inconsistent across MinGw and Clang

Using C++, I'm trying to cast a float value to an int using these instructions :
#include <iostream>
int main() {
float NbrToCast = 1.8f;
int TmpNbr = NbrToCast * 10;
std::cout <...

3votes

1answer

84views

### std::fmod(4.2, 0.12) is equal to epsilon * 1.5

auto a{ 4.2 };
auto b{ 0.12 };
auto result = std::fmod(a, b);
if(result <= std::numeric_limits<double>::epsilon())
result = 0; // <-- This line isn't triggered
In this example, 4.2 is ...

2votes

1answer

175views

### Rational approximation of double using int numerator and denominator in C++

A real world third party API takes a parameter of type fraction which is a struct of an int numerator and denominator. The value that I need to pass is known to me as a decimal string that is ...

2votes

0answers

80views

### Invert the sign of a non-negative floating-point number: Would std::copysign(x) make a difference than (-1)*x?

Let's say we would write a function that would return the inverse of a non-negative floating-point number x. Would the following two functions make a difference in the result (say rounding issues), if ...

2votes

0answers

95views

### Is there options to set on Visual Studio to keep deterministic floating-point results between different multi-thread setup?

I am working with OpenMp with VS2017 in order to compute our statistics with multi-thread process. But, for few values, we found out that results can be differents with the number of threads set. For ...

2votes

0answers

272views

### how to work around boost::multiprecision::cpp_dec_float division error

Division with boost::multiprecision::cpp_dec_float have some kind of rounding error, as follows:
#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
using my_t = boost::...

2votes

2answers

161views

### How to detect differences in floating point behaviour across platforms

What checks can I perform to identify what differences they are in the floating point behaviour of two hardware platforms?
Verifying IEE-754 compliance or checking for known bugs may be sufficient (...

2votes

1answer

568views

### Casting from (long)double to size_t

I'm trying to implement Sieve of Eratosthenes as efficient as possible. I'd like to set length of my prime array to upper bond of
pi(n) < 1.25506n / ln n
but I'm not sure how to proceed with ...

2votes

2answers

735views

### Simple alternatives to floating-point when performing arithmetic on short string-encoded rationals

I am creating unit tests for a function that rounds "rational" numbers stored as strings. The current rounding implementation casts the strings to a floating point type:
#include <boost/...

2votes

0answers

118views

### Finding smallest float x such that x+d=y

How to find the smallest float/double number x which satisfies x + d = y given d and y?
(iiuc this is theoretically solved via setting fesetround (FE_DOWNWARD) and just doing y - d but in clang/Xcode ...

2votes

0answers

350views

### C++ Printing floats with specified number of decimal places

I have numbers stored in a float and I want it to print up to 5 decimal places. The only problem is that I don't want to print 0s afterwards, nor actually modifying the number.
For example:
1 should ...

2votes

0answers

154views

### Is there a warning for floating point equality checking in visual studio (2013) c++

I'm looking for a warning in MS visual studio 2013 similar to Gcc's -Wfloat-equal
I would like to turn on this warning, and compile my code, and find any places that i might need to re-examine for ...

2votes

0answers

337views

### C++ Decimal Floating Point Erroneous Rounding

I am seeing some strange behavior when attempting to use libdfp in a C++ program. Specifically, it appears as if GCC is always rounding to 8 decimal places, even when I use the 64- and 128-bit ...

2votes

0answers

236views

### failure of floating point interrupt MFC Windows 10

We have an MFC product built in Visual Studio 2012/13 in C++ that performs many mathematical calculations. We use floating point interrupts through the coprocessor to help speed up the calculations, ...

2votes

0answers

379views

### Why Floating point exception handling doesn't work in Embarcadero C++ Builder in 64 bit compilation?

I can't make working floating point exception handling in Embarcadero C++ Builder XE8 in 64 bit compilation. 32-bit compilation works fine.
(MS Visual C++ works fine in both 32-bit and 64-bit ...

2votes

0answers

143views

### Stalled cycles due to fldz instruction

I am trying to interpret some perf results on a Xeon x5675 processor. I have a program where a large percentage of the cycles are stalls (from perf stat). Using perf record -e stalled-cycles-...

2votes

3answers

590views

### Converting fl in hexadecimal in c++

I am new to C++, and programming, and I want to write a C++ program to convert a float in hexadecimal with the help of pointers
I've looked on other threads and really tried to get a hold of this but ...

2votes

0answers

817views

### Implementing unordered set of triplets

I have two 3D data-sets. Each element in these sets is a triplet of type (float,float,float). These data-sets have some duplicate elements. I want to merge these two data-sets in such a way so that ...

2votes

5answers

4kviews

### Save float * images in C++

I wanted to understand how I can save an image of type float:
float * image;
Allocated in this way:
int size = width * height;
image = (float *)malloc(size * sizeof(float));
I tried using the ...

2votes

1answer

2kviews

### SSE: convert from const __m128 * to const float *

I'm trying to write a little SSE code but can't continue because of this error:
error C2664: '_mm_loadu_ps' : cannot convert parameter 1 from 'const __m128 *' to 'const float *'
I've to load ...

1vote

0answers

31views

### C++ ignores the second input if the type of the first input in not correct

This is the code:
#include <iostream>
using namespace std;
int main()
{
int var1;
float var2;
cout << "input var1:";
cin >> var1;
cout << "...

1vote

0answers

160views

### Inconsistent results between GCC and Clang for simple floating point calculation

On my Intel x86_64 machine, this C++ code generates different sequences on Clang vs GCC:
#include <iostream>
namespace {
template<typename Out>
constexpr auto caster{[](auto x) constexpr ...

1vote

1answer

65views

### Is it possible to force truncation when instantiating Boost cpp_dec_float from a floating point value?

We are writing software doing arithmetic operations on number with 8 fixed decimal digits (8 decimal digits after the .).
We have been bitten by the limited precision of built-in floating point types ...

1vote

1answer

99views

### Insert FLOAT values into mysql table

I am trying to insert values of type FLOAT into a table using (the C++ connector is in use)
res = stmt->executeQuery("INSERT INTO `ticker_info`(`a1`,`a2`,`a3`,`b1`,`b2`,`b3`,`c1`,`c2`,`v1`,`v2`...

1vote

0answers

56views

### Are there any floating-point requirements concerning precision and range in the C++ standard

The C++ Standard makes the following statements:
[basic.fundamental]: There are three floating-point types: float, double, and long double. The type double provides at least as much precision as ...

1vote

0answers

66views

### Floating point log() * y to integer

Consider the function:
int log2di(double x) {
return (int)log2(x);
}
Since the log2 result is immediately truncated to int, this can be much more efficiently implemented using1 std::ilogb or std::...

1vote

0answers

135views

### C++ issue with istringstream and reading floats

I'm trying to read strings and floats from stdin but I'm running into a weird issue. When reading certain characters the input is "consumed" and lost before being read as a string. I'm expecting that ...

1vote

1answer

52views

### Reading float type numbers from a .txt file and using them in formulas

I am trying to read numbers from a .txt file and use them in formulas within my code and output the results into another .txt file so I can use them easier. My problem is reading the 3000 lines of ...

1vote

0answers

77views

### Is it possible to run custom initialisation code ONCE for each thread created in boost::asio::threadpool

I've noticed that boost::asio::threadpool does not copy threadlocal state such as the floating point environment into the child threads. This caused subtle bugs in my application. My current solution ...

1vote

0answers

123views

### How to print glm vector with manually specified precion?

I know about glm::to_string but I cannot find away to set precision of the string conversion. A few issues come up because of this. If I want to serialize glm structures, I won't be able to store ...

1vote

0answers

248views

### Conversion Precision Error when converting IEE Half Precision Floating Point to Decimal

I have some precision error during the conversion from 16 bit half precision floating point format to decimal. It is able to accurately convert certain numbers while at the same time not accurate for ...

1vote

0answers

59views

### How can we work on fixed amount of precision that is less than double but more than float?

I'm just trying to do something.
There is a some computation to be done using double-type variables.
We have two parameters "x" and "y" where we need to store log2(x) with y-bit precision.
First ...

1vote

1answer

486views

### how to convert python float to bytes to be interpreted in C++ programm

I need to build a python script that export data to a custom file format. This file is then read by a cpp programm (I have the source code, but cannot compile it).
custome file fomat spec:
The file ...

1vote

0answers

45views

### Is there a generic standard way to store floats in a std::set and/or a std::unordered_set?

We know that NaN != NaN for IEEE floats. As a result, a number of "obvious" operations on floating pint numbers have a hidden gotchas in which NaNs in the data can mess things up terribly. For example:...

1vote

0answers

90views

### Does cpp_dec_float give consistent results across platforms?

Can a custom number type such as cpp_dec_float (from Boost) guarantee a consistent floating-point arithmetic behavior (on trigonometric functions such as cos, sin, tan, atan) across different system ...

1vote

0answers

101views

### Union for storing Timestamp. Comparing floats as integers

While trying to wrap my brain around a custom game engine, I stumbled across a piece of code that looks rather odd to me:
union TimeStamp
{
f32 asFloat;
u32 asUInt;
TimeStamp() : asUInt(...

1vote

0answers

693views

### std::ostream, truncate instead of rounding for floating-point numbers

I have my own io-manipulator that prepares a std::ostream stream to print floating-point numbers:
#include <iostream>
#include <iomanip>
inline std::ostream& my_fmt(std::ostream&...

1vote

0answers

1kviews

### Floating point: choose epsilon to prevent zero division?

I have simple formula in my C++ program:
const double z = x / tan(y);
x is number from 1000 to 10 * 1000,
y any angle.
What is the right way to protect the code from zero division?
I mean, I know ...

1vote

0answers

43views

### Should the matrix4 comparison method be consider the floating point comparison logic?

I made a Matrix class. and added some methods for matrix calculation.
I met some problems related with floating point operation.
First time, I made a eqaul method like below:
bool
FloatMatrix4::...

1vote

1answer

321views

### Is double arithmetic the same in cpp and javascript?

Context: I'm trying to port into c++ the implementation of a hash function that uses double arithmetic from its original implementation in JavaScript. In this function, the doubles we are operating on ...

1vote

2answers

286views

### C++ floating point subtraction doesn't appear to occur

I'm trying to create overflow checks for addition (among other operations) and while the unit tests that I wrote for ints and longs worked fine, I've had some trouble trying to extend it to floats. ...

1vote

0answers

149views

### How to build filtered construction predicates in CGAL?

I want to use CGAL's exact predicates inexact constructions kernel, but I would like to use exact arithmetic on few selected Plane_3 constructions where intermediate computation of the plane ...

1vote

0answers

185views

### Reading numbers from txt in C++ (keep same # of decimals)

I have a txt with numbers in this format:
256.6 689.8
460.9 127.2
1027.5 110.5
1516.1 92.6
1319.9 580.3
And the following code in C++ to read them:
Vector<float> txt_data;
fstream myfile(...