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 ?

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

6 Answers 6

2

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

if(float1 < float2) printf("%f < %f\n", float1, float2); 
Sign up to request clarification or add additional context in comments.

3 Comments

Upto what precision would < operator compare the two float values.
@Harman Floating point numbers are totally ordered. Given any two non-special numbers a and b, exactly one of the following is true, a<b, a=b, a>b. No tolerances involved.
Up to the precision which is inherent to float values. This is mostly 5 to 6 decimals.
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.

Comments

0

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

1 Comment

@Harman, I don't understand. Do you have floating point values as text strings? What do you mean by your comment above ("I meant '<'")?
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; } 

2 Comments

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.
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.
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; } 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.