1

I'm trying to write a C program to find the smallest positive integer x so that (1/x) * x is not equal to 1, using single precision. And I do it again with double precision. I know that the x for single precision is 41, however when I test it by writing C code, I still get 1.00000

This is my test code

int main()
{
    float x = 41;
    float div = 1/x;
    float test = div * x;

    printf("%f\n", test);
}

I get 1.0000 instead of a 0.999999

15
  • 1
    please see docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html - they are just a representation of a number Commented Sep 29, 2015 at 19:33
  • @EdHeal how do I get the 0.999999 value? Commented Sep 29, 2015 at 19:34
  • @jqdc2224, try printf("%.10f\n", test); Commented Sep 29, 2015 at 19:36
  • The smallest positive integer is 1. (1/1) * 1 = 1. Why would x/x ever not be one for a real number other than 0? Commented Sep 29, 2015 at 19:36
  • 1
    @EdHeal in that case the document you posted is a must read :-) Commented Sep 29, 2015 at 19:38

1 Answer 1

1

You are not seeing the problem using

printf("%f\n", test);

since the default precision used by printf for floating point numbers is 6. If you increase the precision to 10, you will see that the number is not 1.0.

printf("%.10f\n", test);

prints 0.9999999404 for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.