15,500 questions
4
votes
1
answer
52
views
Why does this PyTorch model produce non-identical outputs on identical inputs even with fixed random seeds?
I am running inference on a trained PyTorch model using the same input tensor, fixed random seeds, and evaluation mode enabled.
import torch
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
model....
24
votes
4
answers
3k
views
What is the difference between iszero(x) and x == 0 for floating types?
C23 added the classification macro iszero for testing whether an argument value is zero, specified like:
int iszero(real-floating x);
What is the difference between using iszero(x) and writing x == 0?...
1
vote
4
answers
292
views
Why doesn't my float comparison work in C?
My expected outcome is that I'm able to compare floats up to the sixth decimal point. The actual results are that they that it's incorrect, only sometimes though.
Bonus if you can tell me why without ...
3
votes
1
answer
261
views
How to deal with large numbers and imprecision in python?
Example:
i = 292893227695
j = 8*i**2 + 1
k = math.sqrt(j)
print(j)
print(k)
print(k.is_integer())
gives output
686291542636760920104201
828427149867.0
True
Even though k is not an integer.
More ...
9
votes
1
answer
186
views
Does std::to_chars ever really disambiguate using round_to_nearest?
[charconv.to.chars] says the following:
The functions that take a floating-point value but not a precision parameter ensure that the string representation consists of the smallest number of ...
7
votes
2
answers
303
views
What is the difference between fmax, fmaximum, and fmaximum_num, and which one should I use?
C23 now has four different ways of computing the maximum or minimum of two floating-point numbers:
x > y ? x : y for maximum (or the other way for minimum)
fmax / fmin
fmaximum / fminimum
...
2
votes
2
answers
248
views
diverging std::atan2's unit of least precision
I write a cross-platform program for Windows and Linux, and I would like it to behave as similar on both platforms as possible. I use some mathematics in the program, e.g. std::atan2 function calls, ...
4029
votes
35
answers
619k
views
Is floating-point math broken?
Consider the following code:
0.1 + 0.2 == 0.3 -> false
0.1 + 0.2 -> 0.30000000000000004
Why do these inaccuracies happen?
25
votes
13
answers
3k
views
How can I parse a string to a float in C in a way that isn't affected by the current locale?
I'm writing a program where I need to parse some configuration files in addition to user input from a graphical user interface. In particular, I'm having issues with parsing strings taken from the ...
3
votes
1
answer
248
views
Accurate computation of the inverse gamma function with the standard C math library
The inverse of the gamma function over the reals is multivalued with an infinite number of branches. This self-answered question is about the principal
inverse of the gamma function, Γ0-1(x), whose ...
2
votes
3
answers
228
views
What are examples of non-finite float values other than infinity and NaN?
C23 §5.2.5.3.3 [Characteristics of floating types <float.h>] paragraph 8 says:
Floating types shall be able to represent signed zeros or an unsigned zero and all normalized floating-point ...
2777
votes
33
answers
4.9m
views
How do I parse a string to a float or int?
How can I convert an str to a float?
"545.2222" -> 545.2222
Or an str to a int?
"31" -> 31
For the reverse, see Convert integer to string in Python and Converting a float to ...
2455
votes
36
answers
6.1m
views
Limiting floats to two decimal points
I want a to be rounded to 13.95. I tried using round, but I get:
>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
For the analogous issue with the standard library Decimal ...
16
votes
2
answers
739
views
Parsing of small floats with std::istream
I have a program that reads the coordinates of some points from a text file using std::istringstream, and then it verifies the correctness of parsing by calling stream's operator bool().
In general it ...
2021
votes
41
answers
1.8m
views
How do I check if a string represents a number (float or int)?
How do I check if a string represents a numeric value in Python?
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
The above works, but it ...