set precision without std::cout

Feb 3, 2018 at 8:13am
Hello! I have a floating point number from which I have to output only the decimals. The problem is that my teacher wants us to output 20 exact decimals from that number, separated by a space. I thought about using setprecision(20), but I don't know how to use it in other way than with std::cout .

1
2
3
4
5
6
7
 for(i=0;i<n;i++)
    {
     L=a[i]/b[i];
     Lt=Lt+L;}
     //I actually intend to attribute these decimals to a variable(if it's 
     // possible)
    fout<<Lt- variable;

I'm using files for I/O



Last edited on Feb 3, 2018 at 8:14am
Feb 3, 2018 at 9:36am
You could use a std::stringstream which is used the same way as std::cout but instead of getting the output printed on the screen you would get it as a string.
Last edited on Feb 3, 2018 at 9:36am
Feb 3, 2018 at 10:36am
Hello flav32,

I am only sure of two ways to set the precision and that is std::cout << std::setprecision(20) or std::cout.precision(20). Since "setprecision" is a stream manipulator it will be be the easiest way to show 20 decimal places.

An alternative would be to put the number into a string and pad the right side of the number with zeros before you print it to the screen.

And if you are not familiar with the "modf()" function from "cmath" header file you can read up on it here http://www.cplusplus.com/reference/cmath/modf/

Hope that helps,

Andy
Feb 3, 2018 at 10:56am
1
2
3
4
5
6
for(i=1;i<=n;i++)
    {
     L=a[i]/b[i];
     Lt=Lt+L;}
     fractpart= modf(Lt,&intpart);
      fout<<setprecision(20)<<fractpart<<" ";

What am I doing wrong? for This input:
n=1
a[i]=7
b[i]=5, I should get as output 4 0 0 0 0 0 ....(20 times), but instead I just get 0..

Feb 3, 2018 at 2:04pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

void print_div_result( unsigned int numerator, unsigned int denominator, unsigned int precision = 20 )
{
    std::cout << numerator << '/' << denominator << " == " ;

    if( denominator == 0 ) std::cout << ( numerator == 0 ? "NaN" : "+infinity" ) << '\n' ;

    else
    {
        // compute the quotient and remainder
        const unsigned int quotient = numerator / denominator ;
        unsigned int remainder = numerator % denominator ;

        std::cout << quotient << '.' ;

        for( unsigned int i = 0 ; i < precision ; ++i )
        {
            remainder *= 10 ;
            std::cout << remainder / denominator ;
            remainder %= denominator ;
        }

        std::cout << "\ntruncated to " << precision << " digits after the decimal point\n\n" ;
    }
}

int main()
{
    print_div_result( 22, 7 ) ;
    print_div_result( 355, 113 ) ;

    print_div_result( 22, 7, 5 ) ;
    print_div_result( 355, 113, 9 ) ;

    print_div_result( 2, 7, 60 ) ;
}

http://coliru.stacked-crooked.com/a/103de26fdd018d95
Feb 3, 2018 at 2:37pm
Here is your code with formatting that reflects the block structure
1
2
3
4
5
6
for(i=1;i<=n;i++) {
     L=a[i]/b[i];
     Lt=Lt+L;
}
fractpart= modf(Lt,&intpart);
fout<<setprecision(20)<<fractpart<<" ";

This will only output one number. Even if you move the closing brace to the end it won't work. Why are you adding L to Lt each time through the loop? Why are you recomputing L each time through the loop? Why are you printing the fractional part when you want to print individual digits (which suggests printing the integer part)?

My advice is to throw away this code and try to write this function again from scratch.
Feb 3, 2018 at 2:54pm
Mr jonathan is a plumber. n represents the number of streets he works on. Each street has one pipe that needs to be repaired. Mr jonathan wrote two numbers.Their ratio represents the lenght of the pipe.
We need to find the total lenght of all pipes (Lt) and output only the decimals of it.


EXAMPLE:
1 street
7 , 5 the sizes of the pipe
This means 7/5=1,4, so we will have 4 0 0 0 0 0 0 ...(20 times) as output.(Notice that the decimals are separated by a space)
Pretty stupid assignment imo...
Feb 4, 2018 at 2:06pm
Look at JLBorges's solution.
Topic archived. No new replies allowed.