C# equivalent to eps(X) function in matlab [duplicate] - c#

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

Related

The double value that I defined to the variable is changing [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 months ago.
I have a problem with conversation of double values.
In the picture Project's framework is .NET6 and i tried on .NET5 and i get same value again on x64.
Can you explain this situation to me?
And how can I get the value unchanged on x64?
Thank you.
This is expected behavior: floating point numbers have a limited number of significant digits, and a number that has a finite number of digits in their decimal representation may require infinite digits in their binary representation.
E.g., the decimal 0.1 is, in binary, 0.00011001100110011.... repeating. Storing this in a float or double, the number of digits is truncated.
Floating point numbers behave similar, but not identical to "real" numbers. This is something you should be aware of.
For financial mathematics, in C#, use the decimal type.
Here you find a good "what every developer should know" overview: https://floating-point-gui.de/.
The standard governing the most common (almost ubiquitous) implementation of floating point types is IEEE 754.

How can the user input an equation for the program to understand? [duplicate]

This question already has answers here:
Evaluate string with math operators [duplicate]
(3 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
OK so I'm fairly new to c# in the big scheme of things. I have encountered one of the first things I'm not 100% sure on how to overcome. At the moment I have a program that asks the user to input a number. This is fine if the user inputs something that can be classified as a double. However, I am unsure how to work around fractions and equations, ie; (5*3)+12 with my code looking like...
Console.Write("Enter an equation: ");
double ans = Convert.ToDouble(Console.Readline());
Console.WriteLine("The answer is " + ans)
This doesn't compute with int or double, also I'm aware that the answer to the example given is a integer, however I want it to be classified as a Double so I can get an exact answer when the equation isn't as clean. How would I get around doing this?

Explanation of why C++ and C# have a different maximum value for type double? [duplicate]

This question already has an answer here:
C++ / C# differences with float and double
(1 answer)
Closed 4 years ago.
In C#, the max value for type double is: 1.79769313486232E+308.
However, in C++, the max value for type double is: 1.79769e+308.
This means that a C++ program (using the strtod function) cannot always parse a double type value which has been output by C#.
Is there any explanation for this behaviour and any good solution to deal with the problem?
From C++ / C# differences with float and double
C++ allows the program to retain a higher precision for temporary
results than the type of the subexpressions would imply. One thing
that can happen is that intermediate expressions (or an unspecified
subset of them) are computed as extended 80-bit floats.
More Info Precision and Accuracy in Floating-Point Calculations
Quote Eric Lippert
section 4.1.6 of the C# specification, which begins **Floating-point
operations may be performed with higher precision than the result type
of the operation. For example, some hardware architectures support an
“extended” or “long double” floating-point type with greater range and
precision than the double type, and implicitly perform all
floating-point operations using this higher precision type. ... ** See
the spec for more details.

how to divide number using double or decimal in c# [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
when divide in c# Answer is different and when i divide from calculator answer is different kindly solve it.
c#
double div=(double)100000/30/9;
Answer 370.37037037037038
370.37037037037038*9*30 //100000.0000000000026
in calculator
Answer 370.3703703703704
370.3703703703704*9*30 //100000
i need exact answer like a calculator.
The difference is simply one of rounding. That is not a rational number, it has a repeating sequence, and cannot be perfectly expressed in binary code. It cannot even be expressed with a finite number of digits in decimal. The only difference there is that your calculator displays fewer digits than the C# double.
The final '04' is simply your calculator rounding '038' upward.

Math.Abs weird behaviour [duplicate]

This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);

Categories