This question already has answers here:
Is Mathf.Approximately(0.0f, float.Epsilon) == true its correct behavior?
(2 answers)
Closed 1 year ago.
Can someone give a simplest example, when Mathf.Approximately gives true?
A try the following and it is always false:
Debug.Log(Mathf.Approximately(0.0001f, 0.0f)); // False
Debug.Log(Mathf.Approximately(0.00000001f, 0.0f)); // False
Debug.Log(Mathf.Approximately(0.00000000000001f, 0.0f)); // False
The documentation says that they're equal if they are "within a very small value (epsilon)".
Epsilon is documented as "the smallest value a float can have different from zero".
How small is that? FloatMinNormal or FloatMinDenormal depending on IsFlushToZeroEnabled, which as you can see from source code would be about 1e-38, much smaller than 0.00000000000001f.
For non-zero values, the exact computation depends on the order of magnitude of the numbers, you can check out the source code for the precise formula.
Some numbers (e.g. 0.1) can't be represented exactly with a float. This is why Approximately can be more useful than straight equality. It is a complex topic, though, that needs more than a SO answer to fully develop. If you want to learn more, maybe start there: https://floating-point-gui.de/
Related
This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 4 years ago.
I'm a bit puzzled by the results of the Math.Floor function in C#.
I get a return of 91 as expected with the call below:
Math.Floor(91.0);
But if I use the call below I get a returned value of 90, while I still expect 91 in this case.
Math.Floor(9.1/0.1);
Is this just due to small rounding errors and is there a way to get consistent results?
Yes, this problem is related to precision, since Math.Floor() simply doesn't round mathematically and always down instead. So even 90.9999999 still results in only 90.
For accurate rounding, use Math.Round() instead or add 0.5 to the value passed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to take a float value, with an arbitrary minimum and maximum possible value, and convert it to a linear scale, for representation on a bar-shaped indicator. The problem is, I can't just lerp it between the minimum and maximum, because the maximum value will always be dramatically higher than the minimum value. I have an array of arbitrary values that I want to act as intermediate points between the minimum and maximum. Now I just need to calculate a logical best-fit curve through the points. Each value is always larger than the last, and the rate of increase in value accelerates the further up you go, but there is no simple formula for calculating this rate of acceleration.
Here's an example of the values that may be used:
6.0, 13.5, 30.0, 75.0, 375.0
where 6 is the minimum, and 375 is the maximum.
If x is exactly one of these values, I would want a simple value depending on how many total values there are, I.E 0, 0.25, 0.5, 0.75, 1. The issue is calculating the in-between values.
How would I go about achieving this? I apologize if a question like this has already been asked, as it feels like a common problem, although I didn't know what to search for. If this has already been answered before, please just point me in the right direction.
Reposting my comment as an answer, as requested.
If a curve might be y(x) = k^(ax+b), take logs of both sides and you have a linear relation. As pointed out tho, this is maths not programming.
I’d pick k = 2, e or 10 for easier implementation; a & b you work out from data.
This question already has answers here:
Listing all permutations of a string/integer
(28 answers)
How can I create cartesian product of vector of vectors?
(9 answers)
Closed 8 years ago.
Here's something which has been bugging me for the past two days.
I need to populate an initial configuration(/state)-space for a fixpoint algorithm.
In this statespace, each transition weight has a vector of weights, and different bounds may apply to each of the weights in this vector.
This is currently defined as an example transition weight being for example (5,-1,-1)
The bounds for each weight correspond to the index of the weight vector itself, for example the upper bounds for these weights, assuming the lower bound is 0 for all is given by (5,3,3)
Now, to set up the initial configuration space, i need to have every combination of weights available in the beginning.
(0,0,0) (0,0,1) (0,1,0) (1,0,0)... and so on, each of them going to their max bounds.
Now, if i was dealing with a 3-weighted system this would be trivial, but i need to support n-dimensional vectors in my code.
So, any ideas as to how i would accomplish populating this configuration space? (I'm using C# currently)
Here's the code for generating all ntuples implemented in Javascript. It's self-explanatory, but if you need further explanation, I'd be glad to help (I actually tried to write the algorithm in pseudocode but what I wrote ended up looking like the comments)
This question already has answers here:
Calculate the unit in the last place (ULP) for doubles
(2 answers)
Closed 9 years ago.
I am trying to translate some matlab code to C# and have hit a problem. Its a numerical algorithm and matlab sets a tolerance which is based on the eps() function.
The matlab documentation (http://www.mathworks.co.uk/help/matlab/ref/eps.html) says:
d = eps(X) is the positive distance from abs(X) to the next larger in magnitude floating point number of the same precision as X. X may be either double precision or single precision
As far as I can tell, there is no native C# function which does the same thing. I am a physicist by trade so the intricacies of floating point operations are not something I really know about. Can someone point me in the right direction?
tl;dr: How to calculate the equivalent of eps(x) in C#?
For completeness, you can compute eps yourself in matlab as follows:
x=1; p=0; y=1; z=x+y;
while x~=z
y=y/2; p=p+1; z=x+y;
end
eps_ = y*2
eps
output:
eps_ =
2.2204e-016
ans =
2.2204e-016
The code is from: Introduction to Scientific Computing, C. F. van Loan
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Precision of Floating Point
Floating point arithmetic is too reliable.
Hi Guys,
I came across a rather strange looking problem, i am running a loop from 82.01 to 169.06 in steps of 0.01 but when i reach 128.01 and do (128.01+0.01) it gives 128.019999999998 instead of 128.02. I am using double for all these computations. If i use decimal to do these computations it works out fine, am i missing a very basic funda here, i found some articles and discussions on the web explaining that decimal is the correct data type to do these computations but still a basic computation like (128.01+0.01) should give correct results.
Floating-point calculations are inherently inaccurate, so, this behaviour is completely normal. If you really need higher accuracy, stick to decimals, but keep in mind that they are way, way slower than floats/doubles. Usually, it's better to just accept the inaccuracies and round as needed.
Here is the scientific detailed explanation of your issue