I want to get every digit from a float, but I don’t want to convert it to a string because is ineffective, because float -> string conversion probably is doing it under the hood (e.g. I found that int -> string is doing exactly that). I am looking for directions for doing it effectively. Question ..

#### Category : floating-point

Meaning, in a "normal" situation where the performance loss of type promotion is irrelevant, where nobody checks for equality on a float and where nobody declares anything funny like f(float x) {/*do something*/}; f(double x) {/*do something else*/}; and so on. I am aware that the general question of "C++ f suffix why?" has been ..

Using long double arithmetic in C++, the number 50,000,056,019,485.52605438232421875 squared yields 2,500,005,601,951,690,788,240,883,712. Meanwhile, the number 50,000,056,019,485.526050567626953125 (which differs from the first number by less than 0.001) squared yields 2,500,005,601,951,690,787,704,012,800, which differs from the first square by almost 1 billion. With the differences highlighted: 50,000,056,019,485.526054382324218750 ^ 2 = 2,500,005,601,951,690,788,240,883,712 50,000,056,019,485.526050567626953125 ^ 2 = 2,500,005,601,951,690,787,704,012,800 Long double’s ..

I am writing some code that needs to represent small (non-zero) probabilities in C++. The code will contain some basic arithmetics (adding and multiplying) with these probabilities. I am aware of (some of) the numerical subtleties that come with representing small numbers (see e.g. Representing probability in C++). I hence was thinking of storing the ..

I have a function that takes in an optional distance parameter dist, but my algorithm works with squared distances. If the user does not specify any value, I want dist to be as large a number as possible. Result foo(double dist = std::sqrt(std::numeric_limits<double>::max())) const; Is the code above safe, or will it blow up because ..

I have a problem with std::stod and std::stof. Whenever I try to convert any string contains float number as a text always decimal part is just cutted. This code float d = std::stof(splittedLine.at(1)); where splittedLine.at(1) is "77.6482" gives me always 77. How to achieve proper value of this string? I need to pass it later ..

I have included the following headers in my simple opengl program: #include <math/math.h> #include <math/vector.h> #include <math/matrix.h> Within my program I want to normalize some vectors as follows: float3 myvec = float3(1.0f, 2.0f, 3.0f); float3 norm = math::normalize(myvec); Doing this gives me several errors: E0304 no instance of overloaded function "math::normalize" matches the argument list ..

In a method like this: vector<uint8_t> Transaction::bytes() { vector<uint8_t> result; … return result; } I need convert a float variable to a vector<uint8_t> (where each byte of the float should be an element of the vector), to allow me add this vector to another: vector<uint8_t> temp; result.insert(result.end(), temp.begin(), temp.end()); Anyone can give a hint of ..

Thanks for looking into my question, I’m trying to solve a problem for which I’ll have to store float’s in 3 different array’s, surprisingly the values being stored are all zeros, could you point out what’s going wrong here. #include <iostream> #include <iomanip> #include <vector> using namespace std; int main(){ int n,m; cin>>n>>m; vector<float>prb(n+1,0),prft(n+1,0),loss(n+1,0); for(int ..

I have a requirement to store very small double numbers into a string format and then later reverse them. However, when I try to run std::to_string() on a small number like 4.7816457028269855e-143 then it simply makes it 0. I referred Set precision of std::to_string when converting floating point values and its linked duplicate. But will ..

## Recent Comments