4

I had to find the result of a function f(x) = x / (1-x)^2 , where 0 < x < 1. The value had to be formatted up to 6 decimal places only.

Here is my C++ code:

float x; scanf("%f",&x);
printf("%.6f",x/((1-x)*(1-x)));

And I did for the same in Python:

 x = float(input()) 
 print ("%.6f" % (x/((1-x)**2)))

For some values of x, both the programs give different answers.

For example, for x = 0.84567,

C++ gives 35.505867 and Python gives 35.505874

Why does this happen?
According to solution, the Python answers are right, while C++ answers are wrong.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483

3 Answers3

4
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>

int main()
{
    const char data[] = "0.84567";
    float x; 
    sscanf(data, "%f",&x);

    double x2;
    sscanf(data, "%lf",&x2);

    std::cout << std::setprecision(8) << (x/((1-x)*(1-x))) << std::endl;
    std::cout << std::setprecision(8) << (x2/((1-x2)*(1-x2))) << std::endl;
}

sample output:

35.505867
35.505874

Conclusion:

Python is using doubles, you're using floats.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Yes, this is obvious if you put it the way you have. The differences are too big for it to be anything else. Have an upvote. – Bathsheba Oct 16 '17 at 13:44
  • You are both completely incorrect here. They are both using float but with different precisions. Look at the code, both are using float. – Eamonn Kenny Oct 16 '17 at 13:47
  • @EamonnKenny: I don't agree. See https://stackoverflow.com/questions/34518653/raising-to-powers-in-c-vs-python/34518772 The `**` forces evaluation in `double` or "higher". – Bathsheba Oct 16 '17 at 13:48
  • @EamonnKenny Per [this](https://stackoverflow.com/a/6663292/4342498) `float` in python is `double` in C++. – NathanOliver Oct 16 '17 at 13:49
  • You are right about the **, but your logic is still wrong. If you use (1-p)*(1-p) you get the same answer. An stackoverflow is not the authority on this, Stroustrup is. There is a difference of precision. And the conclusion above is completely wrong. – Eamonn Kenny Oct 16 '17 at 13:56
  • @EamonnKenny since when is Stroustrup an authority on python? – 463035818_is_not_an_ai Oct 16 '17 at 14:00
  • This problem is language agnostic. Its a numerical analysis issue. The fact that you are seeing it in python is coincidental. Better to remove my posts since nobody appreciates numerical computation anymore. – Eamonn Kenny Oct 18 '17 at 11:16
  • @EamonnKenny I think we're all on the same page, barring terminology. Python is written in C. The `float` datatype in python is implemented as a C `double`, which happens to be the same as a c++ `double`. I think we all appreciate that in c++ both `float` and `double` are floating point types of differing precisions. – Richard Hodges Oct 18 '17 at 11:59
  • What you say now is much clearer. The original statement "python is using doubles, you're using floats" isn't clear enough. If you had have said "Python is using C doubles for its floats, and double double for its double's I would have understood what you meant, but the original statement is ambiguous. Python is using floats for floats, but is its own version of float not C's version of float. Do you see what I'm getting at? – Eamonn Kenny Oct 18 '17 at 12:45
  • We are basically saying the same thing, but are communicating it completely different ways. – Eamonn Kenny Oct 18 '17 at 12:47
3

Python has implemented IEEE 754 double-precision, so its output is closer to real answer.

From documentation: https://docs.python.org/3/tutorial/floatingpoint.html#representation-error

Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”.

In C++ float is single-precision. Using double instead of float should give you similar output.

Jacek
  • 829
  • 12
  • 31
3

As others have pointed out, floating point numbers in python are implemented using double type in C. See section 5.4 of the Python Documentation.

Running this example on Coliru:

#include <cmath>
#include <cstdio>

int main()
{
    float pf = 0.84567f;
    printf("%.6f\n",pf/((1-pf)*(1-pf)));

    double pd = 0.84567;
    printf("%.6f\n",pd/((1-pd)*(1-pd)));

    return 0;
}

demonstrates the difference:

35.505867
35.505874