473,378 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Finding the smallest of two double values

94 64KB
Hello, everyone. What would be a good way to find the smallest of two double values in C? Thanks in advance!
Jul 6 '13 #1

✓ answered by donbock

That formula is a variation of a common method used to determine if a1 and a2 can be considered equal. It does not tell you which one is larger if they are not equal.

In mathematics, exactly one of the following statements is true for any two real numbers a1 and a2:
  • a1 < a2
  • a1 = a2
  • a1 > a2

A similar list for floating-point numbers is:
  • a1 < a2
  • a1 ≈ a2 (a1 is approximately-equal to a2)
  • a1 > a2
  • a1 and a2 are incommensurable (one or both are not-a-number)

If your intent is simply to return the smaller value (and you're sure you don't have to worry about the NaN case), then go ahead and use comparison operators. You can't predict which value is returned when the numbers are nearly equal, but that shouldn't matter because the values are nearly equal.
Expand|Select|Wrap|Line Numbers
  1. if (a1 < a2)
  2.    return a1;
  3. else
  4.    return a2;
  5.  
If your intent is to select different execution paths for less-than, nearly-equal, and greater-then (and you're sure you don't have to handle the NaN case) then you need to be more careful. Look at Comparing Floating Point Numbers for a discussion of what the AlmostEqualRelative function has to be. (By the way, the almost-equal leg has to come first in the code snippet.)
Expand|Select|Wrap|Line Numbers
  1. if (AlmostEqualRelative(a1,a2,epsilon))
  2.    <nearly-equal execution path>
  3. else if (a1 < a2)
  4.    <less-than execution path>
  5. else
  6.    <greater-than execution path>
You'll have to handle the incommensurable (NaN) case to be fully complete. I'll leave that for another time. (By the way, I can't recall if the Infinities also bring you to that incommensurable case.)

The most valuable thing you'll read in the linked article comes near the beginning: "[Floating-point] math is hard."

3 1611
weaknessforcats
9,208 Expert Mod 8TB
You can't compare floating point values directly. You can try this:

fabs(a1-a2) < fabs(a2)*epsilon

Here, the absolute value of the difference between a1 and a2 is compared to the absolute value of one of the numbers multiplied by some epsilon (a small value you decide is "close enough". If the above is true a1 is larger than a2.
Jul 6 '13 #2
stdq
94 64KB
Thank you very much!
Jul 6 '13 #3
donbock
2,426 Expert 2GB
That formula is a variation of a common method used to determine if a1 and a2 can be considered equal. It does not tell you which one is larger if they are not equal.

In mathematics, exactly one of the following statements is true for any two real numbers a1 and a2:
  • a1 < a2
  • a1 = a2
  • a1 > a2

A similar list for floating-point numbers is:
  • a1 < a2
  • a1 ≈ a2 (a1 is approximately-equal to a2)
  • a1 > a2
  • a1 and a2 are incommensurable (one or both are not-a-number)

If your intent is simply to return the smaller value (and you're sure you don't have to worry about the NaN case), then go ahead and use comparison operators. You can't predict which value is returned when the numbers are nearly equal, but that shouldn't matter because the values are nearly equal.
Expand|Select|Wrap|Line Numbers
  1. if (a1 < a2)
  2.    return a1;
  3. else
  4.    return a2;
  5.  
If your intent is to select different execution paths for less-than, nearly-equal, and greater-then (and you're sure you don't have to handle the NaN case) then you need to be more careful. Look at Comparing Floating Point Numbers for a discussion of what the AlmostEqualRelative function has to be. (By the way, the almost-equal leg has to come first in the code snippet.)
Expand|Select|Wrap|Line Numbers
  1. if (AlmostEqualRelative(a1,a2,epsilon))
  2.    <nearly-equal execution path>
  3. else if (a1 < a2)
  4.    <less-than execution path>
  5. else
  6.    <greater-than execution path>
You'll have to handle the incommensurable (NaN) case to be fully complete. I'll leave that for another time. (By the way, I can't recall if the Infinities also bring you to that incommensurable case.)

The most valuable thing you'll read in the linked article comes near the beginning: "[Floating-point] math is hard."
Jul 8 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code...
4
by: MFRASER | last post by:
How can I compare two double values and return the greater of the two values? example double doublea = 1.0; double doubleb = 2.0 double retVal = ?compare doublea and doubleb
19
by: Erich Pul | last post by:
hi! i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences...
0
by: Peter Stojkovic | last post by:
I am using VB.2005 When I stream a class of several double values into a XML-string, than the values are very long : for example 3.00000000001 or 12.4324634547 Is there any possibility, to limit...
19
by: nickyeng | last post by:
i have a question from my paper, which i dont have the correct answer, so here i asking anyone who can help, provide me the appropriate answer. ----------------------------------- Write a class...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
5
by: ahsuman | last post by:
Hai , This is a bit weird for me.It may be that i got it wrong, but i request you to respond please. All i need to do is add two double values Dim a as double =324.0 Dim b as double=0.1...
4
by: b4ukiran | last post by:
Hi all, I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I...
3
by: karmjit435 | last post by:
I have a c++ component. It exposes an API which accept two double values: say: void Multiply(double a, double b); if i call this API from a C# code using PInoke and pass the values a=199.4225...
4
by: Pam Golembiewsk | last post by:
I declared a double called annualIntRt earlier in my program, so other modules could access the data. The file is being read as double values, all I have to do is fill my array with them, but I am...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.