0

I want to compare float values to determine the biggest number in the list.The precision of the float is fixed, 6 after the decimal.

Should I just compare them as integer and if they are equal then go and dig the values after the decimal ?

Harman
  • 1,571
  • 1
  • 19
  • 31
  • What exactly do you mean by "compare them as integer and if they are equal then go and dig the values after the decimal"? – sth Oct 13 '11 at 15:37
  • I mean compare them using "==" sign first, and if they are still the same, then do something else(not sure what). – Harman Oct 13 '11 at 15:39
  • If they are the same, they are the same. What else would you do then? – glglgl Oct 13 '11 at 15:54
  • Try putting up some lines of code, just to show us what these "floats" are. – xanatos Oct 13 '11 at 16:06

6 Answers6

2

The easiest way to compare floats is with the < operator, like so

if(float1 < float2)
     printf("%f < %f\n", float1, float2);
Dave
  • 10,964
  • 3
  • 32
  • 54
  • Upto what precision would < operator compare the two float values. – Harman Oct 13 '11 at 15:47
  • @Harman Floating point numbers are totally ordered. Given any two non-special numbers a and b, exactly one of the following is true, ab. No tolerances involved. – David Heffernan Oct 13 '11 at 15:52
  • Up to the precision which is inherent to float values. This is mostly 5 to 6 decimals. – glglgl Oct 13 '11 at 15:54
1

use DBL_EPSILON as a basis for how close to doubles need to be.

use FLT_EPSILON exists for floats.

see this answer for a sample function that demonstrates the technique.

Community
  • 1
  • 1
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0

Following article provides various alternatives on comparing float values.. http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Usually, finding the difference between the floats and checking if the difference is within the precision limit, helps in comparing the floats. But as also mentioned, there is no perfect answer and implementation for this because different between adjacent floats varies with the magnitude. Hence if you know the range of values which your program will use, then you can chose appropriate implementation for comparison.

pekon
  • 31
  • 2
0

That depends on things you don't tell us.

  • How are these numbers provided to you? As strings, or are they already in the memory (float array/double array)?
  • How large are they? Is there any greatest boundary, in order to have a certain precision? I.e., are the 6 decimals relevant if the number is, let's say, 100 millions?
  • Is it ok to lose precision?

etc.

As you have the values in a float array, I would do the following:

float biggest(float * values, int num)
{
    int i;
    float curmax;
    if (num == 0) return NAN;
    curmax = values[0];
    for (i=1; i < num; i++) {
        if (values[i] > curmax) curmax = values[i];
    }
    return curmax;
}
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Numbers are in a float array. Numbers are mostly not large but there is no user defined greatest boundary specified. The 6 decimals is always relevant. and no, it is not ok to loose precision. – Harman Oct 13 '11 at 15:47
  • ok, as you have a float array, you already might have lost (not loost) precision. In this case, I would just define a variable and iterate over the list, updating the variable whenever you find a value greater than the variable. – glglgl Oct 13 '11 at 15:51
0

If you want to find the biggest number, why would you need to worry about comparing for equality?

Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • @Harman, I don't understand. Do you have floating point values as text strings? What do you mean by your comment above ("I meant '<'")? – Kusalananda Oct 13 '11 at 15:56
0

Code to do this looks like the following:

float MinFloatArray(float *a, int n)
{
    int i;
    float result = MAX_FLOAT;
    for (i=0; i<n; i++)
        if (a[i]<result)
            result = a[i];
    return result;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490