Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3695566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:38:00+00:00 2026-05-19T04:38:00+00:00

I need to find maximum and minimum of 8 float values I get. I

  • 0

I need to find maximum and minimum of 8 float values I get. I did as follows. But float comparisons are going awry as warned by any good C book!
How do I compute the max and min in a accurate way.

main()
{
    float mx,mx1,mx2,mx3,mx4,mn,mn1,mn2,mn3,mn4,tm1,tm2;

    mx1 = mymax(2.1,2.01); //this returns 2.09999 instead of 2.1 because a is passed as 2.09999.
    mx2 = mymax(-3.5,7.000001);
    mx3 = mymax(7,5);
    mx4 = mymax(7.0000011,0); //this returns incorrectly- 7.000001
    tm1 = mymax(mx1,mx2);
    tm2 = mymax(mx3,mx4);
    mx = mymax(tm1,tm2);

    mn1 = mymin(2.1,2.01);
    mn2 = mymin(-3.5,7.000001);
    mn3 = mymin(7,5);
    mn4 = mymin(7.0000011,0);
    tm1 = mymin(mx1,mx2);
    tm2 = mymin(mx3,mx4);
    mn = mymin(tm1,tm2);


    printf("Max is %f, Min is %f \n",mx,mn);

    getch();
}

float mymax(float a,float b)
{
    if(a >= b)
    {
        return a;
    }
    else
    {
        return b;
    }
}

float mymin(float a,float b)
{
    if(a <= b)
    {
        return a;
    }
    else
    {
        return b;
    }
}

How can I do exact comparisons of these floats? This is all C code.

thank you.

-AD.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:38:00+00:00Added an answer on May 19, 2026 at 4:38 am

    You are doing exact comparison of these floats. The problem (with your example code at least) is that float simply does not have enough digits of precision to represent the values of your literals sufficiently. 7.000001 and 7.0000011 simply are so close together that the mantissa of a 32 bit float cannot represent them differently.

    But the example seems artificial. What is the real problem you’re trying to solve? What values will you actually be working with? Or is this just an academic exercise?

    The best solution depends on the answer to that. If your actual values just require somewhat more more precision than float can provide, use double. If you need exact representation of decimal digits, use a decimal type library. If you want to improve your understanding of how floating point values work, read The Floating-Point Guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find out the maximum and minimum value in a line by
I need to find the maximum and minimum of an arbitrary C expression which
There is an ArrayList which stores integer values. I need to find the maximum
I was wondering how can I find minimum and maximum values from a dataset,
I cannot get this to work. I need to first find the maximum sum
I need to find the maximum route of a triangle. I earlier posted this
How to find the maximum of two explicit values in MySQL? Something like MAXIMUM(1,
I am working on a project where I need to get the minimum and
I need to find the maximum date for that particular client, that will be
I have a nxm matrix and I need to find the maximum of sum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.