3

I'm starting to learn cpp and am trying some of the cpp questions on hackerrank.com as a practice but I'm not using their compiler but I'm using visual studio 2019.

The issue is 19856.992 is printing as 19857 and -5279235.721231465 as -5.27924e+06.

Code:

#include<iostream>

int main() {
    int intVal;
    long long int LongIntVal;
    char CharVal;
    float FloatVal ;
    double DoubleVal;
    std::cout << "input: ";
    std::cin >> intVal >>LongIntVal>>CharVal>>FloatVal>>DoubleVal;
    std::cout << std::endl << intVal << std::endl << LongIntVal << std::endl<< CharVal << std::endl << FloatVal << std::endl << DoubleVal;
    return 0;
   
}

output:

input: 211916801 97592151379235457 p 19856.992 -5279235.721231465

211916801
97592151379235457
p
19857
-5.27924e+06
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Read this: https://stackoverflow.com/questions/1458923/long-long-in-c-c#:~:text=long%20long%20is%20not%20part,represent%20the%20number%2C%20long%20otherwise. And you could have used `long int` instead. – TechGeek49 Feb 04 '21 at 14:51
  • 2
    It only prints 5-6 significant digits for floats and doubles. If you want more, you need to set the precision. Just do an internet search on cout and setprecision – cup Feb 04 '21 at 14:52
  • float has only ~7 digits of precision so obviously 19856.992 will be stored as ~19856.99... in memory and printed as 19857 after rounding – phuclv Feb 04 '21 at 17:06

2 Answers2

1

You can make use of std::setprecision() function under iomanip header file

I have altered your code so that you can see how to make use of it:

#include <iostream>
#include <iomanip>
int main()
{
    int intVal;
    long long int LongIntVal;
    char CharVal;
    float FloatVal;
    double DoubleVal;
    std::cout << "input: ";
    std::cin >> intVal >> LongIntVal >> CharVal >> FloatVal >> DoubleVal;
    std::cout << std::endl
              << intVal << std::endl
              << LongIntVal << std::endl
              << std::setprecision(20) << CharVal << std::endl
              << std::setprecision(20) << FloatVal << std::endl
              << std::setprecision(20) << DoubleVal;
    return 0;
}

Output:

input: 211916801 97592151379235457 p 19856.992 -5279235.721231465

211916801
97592151379235457
p
19856.9921875
-5279235.7212314652279
Tharun K
  • 1,160
  • 1
  • 7
  • 20
1

I suggest you could output the value you want by calculating the decimal places of the floating point number and combining the function setprecision.

For example:

int foo(float f)
{
    int i = 0;
    while (i++, f != (int)f) f *= 10;
    return i - 1;
}

int main()
{
    float f = 221.444356;
    int n = foo(f);
    cout << setiosflags(ios::fixed);
    std::cout << n << std::endl << std::setprecision(n) << f;
}

Ouput:

5
221.444356

cout.precision(n), setprecision(n) can control the number of floating-point numbers displayed in the output stream. The C++ default stream output value valid bit is 6.

If setprecision(n) is combined with setiosflags(ios::fixed), you could control the number of digits to the right of the decimal point. setiosflags(ios::fixed) is a fixed-point representation of real numbers.

If it is combined with setiosflags(ios::scientific), you could control the number of digits after the decimal point of the coefficient in the exponential representation. setiosflags(ios::scientific) is an exponential representation of real numbers.

If setprecision(n) is set, but setiosflags(ios::fixed) is not set, it means the total number of digits when displaying regular floating-point numbers.

If setprecision(n) is set, but setiosflags(ios::scientific) is not set, when the floating-point number is large, the system automatically displays the exponential form, which refers to the total number of digits in the coefficient

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7