Clear Filters
Clear Filters

what does eps(x) do ?

93 views (last 30 days)
dareen
dareen on 15 Mar 2024
Edited: Hassaan on 15 Mar 2024
i read the documentation but i don't really understand how it works and what is does ? if you can explain how the values are got ?
what does the distance between x and the next floating number mean?
>> eps(2)
ans =
4.440892098500626e-016
>> eps(20)
ans =
3.552713678800501e-015
>> eps(2000000000)
ans =
2.384185791015625e-007

Accepted Answer

Hassaan
Hassaan on 15 Mar 2024
The function eps in MATLAB, which stands for epsilon, is used to determine the relative accuracy of the floating-point arithmetic of the system you're using. More specifically, eps(x) returns the distance between x and the next larger in magnitude floating-point number of the same precision as x. This value represents the smallest difference that the system can distinguish from x given its floating-point arithmetic system.
eps(2):
  • The result is approximately 4.440892098500626e-016.
  • This means that in the floating-point representation of numbers, the smallest value that can be added to 2 to get a distinguishably different number (according to the computer's arithmetic system) is about 4.44e-16. Any number smaller than this added to 2 would still be considered 2 by the system due to the limits of precision.
eps(20):
  • The result is approximately 3.552713678800501e-015.
  • Similarly, for the number 20, the smallest increment that can be recognized by the system is about 3.55e-15. Again, this is the smallest difference from 20 that the system can recognize as a different number.
eps(2000000000) (2 billion):
  • The result is approximately 2.384185791015625e-007.
  • For much larger numbers, like 2 billion, the smallest distinguishable increment becomes larger, in this case, about 2.38e-7. This is because floating-point precision is relative; as numbers get larger, the precision in terms of absolute difference also becomes larger.
The precision or epsilon value depends on the magnitude of the number you're looking at because of how floating-point numbers are stored in computers. Computers use a finite number of bits to represent real numbers, leading to a trade-off between the range of values that can be represented and the precision of those values.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
dareen
dareen on 15 Mar 2024
could not ask for a more clear answer really , "This value represents the smallest difference that the system can distinguish from x given its floating-point arithmetic system." this really cleared a lot of stuff , if i may check ,from what i understood if i compare 2 numbers using the == compare the system may not distingush the two numebrs from each other so if i have a=20 and b=20.00000001 i do abs (b-a)<eps(20) if true they are the same or do i just compare it with eps?
Hassaan
Hassaan on 15 Mar 2024
Edited: Hassaan on 15 Mar 2024
For a more general-purpose comparisons, especially when you're not sure of the exact magnitudes involved or when you're dealing with very precise calculations, a more conservative approach might involve comparing against a scaled epsilon. For example:
tolerance = max(eps(a), eps(b)) * someFactor;
if abs(a - b) < tolerance
% Consider a and b to be equal
end
In this approach, someFactor is a scaling factor that you can adjust based on the precision needed for your application. This method takes into account the precision of both numbers being compared and scales the tolerance accordingly, making it a robust way to compare floating-point numbers across a wide range of values.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!