Whats wrong with setprecision in my compiler? - c++

I've been having trouble with my compiler, NetBeans. It seems that using setprecision is not doing anything. Here is the code needed to see what's happening. This part of the program check to see if a triangle it possible given three sides.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
double SideA,SideB,SideC,p,a,longest;
cout<<"Enter the length of each side of your triangle. Press enter after each number."<<endl;
cin>>SideA>>SideB>>SideC;
cout<<fixed<<setprecision(3);
if(SideA+SideB<=SideC||SideA+SideC<=SideB||SideB+SideC<=SideA||SideA<=0||SideB<=0||SideC<=0)
{
cout<<"The length of the first side is "<<SideA<<"."<<endl;
cout<<"The length of the second side is "<<SideB<<"."<<endl;
cout<<"The length of the third side is "<<SideC<<"."<<endl;
cout<<"This triangle is impossible."<<endl;
}
return 0;
}
My problem is that I want the side length to be rounded to the nearest thousandths when shown on the screen. It does not do this with my NetBeans compiler. I've put this into online compilers, and it works there.
Here's my C++ configuration in NetBeans:
The sample input data is: 6.6666666, 5.4444444, 1
The expected output is: 6.667, 5.443, 1.000
The actual output is: 6.66667, 5.44444, 1

Your code is behaving correctly. You are using cout to display the values of double variables, and by default it will use a precision of 6:
A double value will be printed using a general format that usually
works fine to represent a number in a small amount of space. The basic
amount of space used is determined by the precision. The default
precision is 6, which means up to 6 significant digits are used to
represent the number. This counts digits both to the left and the
right of the decimal point. Fewer digits are printed if the result is
accurate; e.g. trailing zeros at the right of the decimal point are
not printed. As shown in the example below, as the number increases in
size, places to the right of the decimal point will be dropped, and
the result rounded off as needed to stay within 6 digits.
The text in bold font explains the output you obtained for your sample input: 6.66667, 5.44444, and 1.
You can limit the precision to obtain the results you want by using cout.precision() or setprecision(). This is also detailed in the linked article.
Finally, note that this is issue has nothing to do with NetBeans. You can confirm this by using g++ to compile your code from the command line. When you run it you will get the same results, even though you are not using NetBeans.

Related

Program is not accessing correct index of array.. Why?

I come to this site in need of help, after struggling with this problem for a few days now. I am trying to program a poem that accepts some data from standard input and then outputs a poem based on that data.
The code seems to be working, but it is not correct! It is giving me the wrong index of the array I am using. I would love extra eyes to help me with my code and let me know what I am doing wrong.
ALSO! For some reason, I am not able to access the third array of the char array... I tried to place "SIZE - 1" in there but it prints nothing... Would love to understand why this is. Does this look right?
// Program that accepts some data from standard input,
#include <iostream>
#include <cstring>
//here... extracted.
for (int sign = 0; sign < poem[line]; sign++)
{
if (line > word_count)
{
std::cout << " ";
print_poem(seed);
}
else
{
print_poem(seed);
}
You haven't mentioned what exactly the task is but I can at least explain to you parts of the problem.
Are the correct syllables being printed?
Let's assess if the correct syllables are being printed. I ran your code on my machine (with the input you provided that is "100 3 1 5 7 5") and got:
nahoewachi
tetsunnunoyasa
munahohuke
The syllable count of each line is fine (5,7,5) so that's not a problem.
The first syllable you have a problem with is chi in nahoewachi. I'm only illustrating why this syllable is being printed. You can apply the same logic to the rest.
Initially, the seed is 100. Before processing the first row, you apply generate_prnd, which gives 223. Before calculating chi, you print 4 other syllables (na, ho, e and wa). This means that you have applied generate_prnd 8 times before calculating the fifth syllable.
Applying generate_prnd 8 times on 223 gives 711. Applying one more time (to get row) gives 822.
822%9 = 3rd row (0 indexed).
Applying one more time (to get column) gives 361. 361%5 = 1st column.
Therefore the index for the fifth syllable is (3,1). The string at the (3,1)th index is "chi". Therefore, the correct syllable is being printed. The indexing is correct. There's a problem with your logic if you want a different syllable to be printed.
Now, let's assess why there aren't any spaces in your output.
In the example you provided, num_lines=3. The word_counts (actually syllable counts) are 5, 7 and 5. You are applying a space when line (which is always less than num_lines) is greater than word_count.
However, line is always less than word_count since the maximum value of line is 2 (num_lines - 1). Therefore, a space will never be printed.
P.S. If you are allocating memory using new, don't forget to deallocate using delete later.

C++ Xtensor increase floating point significant numbers

I am building a neural network and using xtensor for array multiplication in feed forward. The network takes in xt::xarray<double> and outputs a decimal number between 0 and 1. I have been given a sheet for expected output. when i compare my output with the provided sheet, I found that all the results differ after exactly 7 digits. for example if the required value is 0.1234567890123456, I am getting values like 0.1234567-garbage-numbers-so-that-total-numbers-equal-16, 0.1234567993344660, 0.1234567221155667.
I know I can not get that exact number 0.1234567890123456 due to floating point math. But how can I debug/ increase precision to be close to that required number. thanks
Update:
xt::xarray<double> Layer::call(xt::xarray<double> input)
{
return xt::linalg::dot(input, this->weight) + this->bias;
}
for code I am simply calling this call method a bunch of times where weight and bias are xt::xarray<double> arrays.

adjust the user input in the C++

I'm writing a C++ program for bank to withdrawal money , in that code I used fixed and set precision to change the output for two decimal point but I don't want to do that , I want to make sure the user only need to input in decimal number I mean like only two decimal number .
I want like he/she can't able to enter more than two decimal number .00
You can't do this. At least not with standard input and CPP uses the same for taking input.
The best you can do is to take input into a value and cut off the decimal part after the second digit using setprecision(2), or truncate the value once you have it in the variable.
Hope it helped you.

How can I take a float value and print it as it is in c++(5.0 as 5.0)

In one problem I am receiving such inputs and I need to output them as it is.
For example I will receive 5.0 as input and need to print 5.0 as output,
but it is printing 5. I used setprecision which is not working .
I think the thing is
for numbers like 2.0,3.0,4.0 it is rounding off while taking input itself.
Please help me.
See my code:
float n=5.0
// n*=1.0;
cin>>n;
cout<<setprecision(16);
cout<<n;//its printing 5 here
/*
I also tried
printf("%f",n);
//but its printing 5.000000
I can't use printf("%.1f",n)<br>
as input can be 5.234 or 4.5687 so making %.1f will not work for others
*/
You can use std::fixed. This will, like printf do, force to print all the precision decimals. This means that 5 is now printed 5.000000, if you don't mind more than one unneeded 0.
If you do mind, I don't see many solutions, but to write the string in memory with an ostringstream or with sprintf, then to append ".0" if you cannot find a point in it, and to print that string.

Fortran output exponentials to file

I'm having trouble writing exponential numbers to a file. If I set output to be in the form E20.8 and have numbers in the range e-99 to e+99, I'm fine. When I try to output a number less than e-99, such as 1.23456e-100, I get 1.23456000-100 instead (dropping the e, zeros because of E20*.8*). This is problematic for post-processing.
Any suggestions for a fix? Is there another parameter for the Ew.d format for the size of the exponential?
I wasn't persistent enough in my searching: the full output format is Ew.d followed by "e" and the number of spaces to leave for the exponential. In my case, E20.8e3 worked great. The answer, for future reference, is here:
http://www.hicest.com/Format.htm