0

I don't know why my code is giving to me incorrect results.

When I put a number like 6670680902 the result is 6.67068e+0.7 (which is 66706800). That is not the correct result. When I use the calculator the correct result of 667006080902 / 100 is 66706809.02.

What should I do to fix it?

#include "stdafx.h"
#include "conio.h"
#include "iostream"

using namespace System;
using namespace std;

int main()
{
    float a;
    float b;

    cout << "Ingrese el codigo: "; cin >> a;

    b = a / 100;

    cout << "result: " << b;

    _getch(); 
    return 0;
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You do realize that `float`s have only seven, or so, significant digits, right? – Sam Varshavchik Apr 12 '18 at 01:41
  • Floats are specifically less precise to save memory, they should not be used for highly precision expected answers. Instead use a double or long double. See: https://stackoverflow.com/questions/2386772/what-is-the-difference-between-float-and-double – Spencer Wieczorek Apr 12 '18 at 01:42
  • 6
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Spencer Wieczorek Apr 12 '18 at 01:45
  • i tried with the double and long double type and still wrong result. – Ernesto Pacheco Apr 12 '18 at 01:48
  • 1
    @ErnestoPacheco By default C++ output automatically makes large numbers into scientific notation, it does not mean `6.67068e+0.7` is `66706800`. See: https://stackoverflow.com/questions/2335657/prevent-scientific-notation-in-ostream-when-using-with-double – Spencer Wieczorek Apr 12 '18 at 01:52
  • C++ is not C, please don't spam tags – Passer By Apr 12 '18 at 01:53
  • Seems fine to me, using your number and a few others. https://ideone.com/vBlTzG – Retired Ninja Apr 12 '18 at 01:56
  • @RetiredNinja Your using a `double`, a `float` which OP is using will not have the correct value. OP is also not making `cout` use precision printing, making it show as scientific notation. – Spencer Wieczorek Apr 12 '18 at 01:58
  • @SpencerWieczorek OP stated he used doubles and still had trouble. Asserting that the program doesn't match a calculator seems like an issue with printing the numbers. I chose to avoid that issue. – Retired Ninja Apr 12 '18 at 02:12
  • @RetiredNinja It's both using a `float` and printing the numbers as scientific notation as I mentioned a few times above. The reason why OP says it gives the same result is because they are printing it as scientific notation, even if the number is actually correct. – Spencer Wieczorek Apr 12 '18 at 02:15

2 Answers2

3

The first issue here is by default C++ will show larger numbers using scientific notation, there are ways to prevent this for floating point numbers like floats. One simple way is to add << fixed before your number:

cout << "result: " << fixed << b;

Which will return 66706812.0.

The next problem is that floats are not good at being precise, which is why the number still isn't correct. Floats are less precise compared to something like a double which has twice the precision. If you use a double instead for a and b:

int main()
{
    double a;
    double b;
    //...
    cout << "result: " << fixed << b;
    //...
}

you will get the value you expect: 66706809.02

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
1

Can do by using 'limits`

#include "iostream"
#include <string>
#include <limits>

using namespace System;
using namespace std;

int main()
{
 double a;
 double b;

 cout << "Ingrese el codigo: "; cin >> a;

 b = a / 100;

  cout.precision(numeric_limits<double>::digits10 + 1);

  cout << "result: " << b << endl;
  _getch(); 
 return 0;
 }

Output:

result: 66706809.02
ntshetty
  • 1,293
  • 9
  • 20