Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How can I add a scientific and a float number ? I have something like
var1 0.99999899 var2 3.5008552e-05 sum 3.5008552e-05
but what I dont understand is why in the first place var2 is shown as scientific while in the first place I declared
double var1, var2;
so, actually their sum is just var2...
thanks
a
The way a floating-point value is displayed is down to the mechanism by which you display it. It is not a property of the value itself, nor is it in any way stored within the variable:
A number is a number is a number. What you call "scientific" is not a class of numbers. It's a class of representations of numbers. The same way that "twelve" and "12" and "XII" and "a dozen" and "IIIIIIIIIIII" all represent the same number. This "scientific" thing only exists when you decide to represent the number in some specific way (i.e. when you output it). Calculations don't "turn numbers into scientific" the same way that saying that "2 * 6 is twelve" doesn't turn numbers into English words. The variables always store the numbers not the representations. — R. Martinho Fernandes
Your display mechanism — std::cout? — is choosing the best way to output the value. You can override this with IO manipulators such as std::fixed, though it's pretty fiddly sometimes to get it just how you want it, due to library limitations.
If you have <stdio.h> available then you may use int printf ( const char * format, ... ); the format specifier would look something like this: printf("%.5f", your_double) where 5 is the number of digits you want after the decimal point, the default is 6.
(printf docs)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
in c++, When I declare a variable of the type of integer and give it a numerical value, for example, and when printing the variable, its numerical value appears, but when declaring a variable of the character type with the same value of the integer, the return value is a symbol, how is that.
In C and C++ a char is an integer. It is a number, just like short, int, long, etc. One difference is that you don't know if char is signed with a range from -128 to 127, or unsigned with a range from 0 to 255.
However, even though a char is a number, it is usually used to represent an ASCII character value. So, that is what it defaults to. When you write std::cout << 'c' << std::endl; that 'c' is written as a 'c' because that is probably what the programmer wanted to do. To get it to output as a number, you could do std::cout << static_cast<int>('c') << std::endl;
Everything is numbers. Chars are numbers mapped to ASCII (1 byte per character) or UNICODE, which allows to represent almost any character of any language, but using more bytes.
Integers (and floats, longs, etc) are those numbers used for mathematical operations or a representation of themselves.
When you print something to screen the functions printf or cout take numbers (ints or chars) and display them as characters (letters or numbers) through pixel accommodation or a similar mechanism, whose specific dynamic it's not relevant at this point.
Declaring char = 65; it's the same as saying char = 'A'; because at the end of the day the information on a char is stored and processed as a number.
This question already has an answer here:
Set precision of std::to_string when converting floating point values [duplicate]
(1 answer)
Closed 6 years ago.
I am using C++11. I have real world coordinates such as 38.0098662, 23.7805398 in a text file, meaning 2 double variables. I parse that file with C++ and I convert the strings to double with the stod() method. When I tried printing the values with count.precision(9);, the values seemed to be the same as in the text file.
Then, I want to convert those double values back to string format. For that I use to_string() method. However that seems to be changing the value slightly. For example:
38.0098662, 23.7805398 became
38.009866, 23.780540
// in fact, every number is xx.yyyyyy
I don't understand why this is so hard to do with C++11. Why do I lose precision for no reason ? How can I do this properly ? Every question I looked into pointed to to_string() method, which is what I am using.
Someone had the same problem as you had.
Simply set the precision of the std::ostringstream to your desired size and return it as std::string.
Here is a reference to an older question which shows the implementation:
Look at this!
Hope this helps!
It cause from compiler code optimization(for fastest run).
Sometimes C++ compiler optimizes floating point number processing, and loses a bit precision.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm brand new to programming so please answer in simple terms.
I'm trying to print a double. The program ask the user to input as many digits of pi as he can remember, and then it is supposed to print it back to him. But it always prints back 6 decimal places. I need it to print the amount of decimals that were originally put in. so 3.14 is to decimals, while 3.141592654 is 9. so it prints what was put in.
You need a combination of std::fixed followed by std::precision
Like this:
double f =3.14159;
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // prints 5 decimals
std::cout << std::setprecision(9) << f << '\n'; // prints 9 decimals
Now what to pass to std::setprecision() as an argument needs to be calculated from the input you are getting.
Numbers are numbers, not strings. They have actual precision governed by their type (float, double etc).
You cannot "remember" the logical precision originally provided, unless you take input as string, count the precision yourself, then convert to a number.
Then you use formatting options to reproduce that precision level in the output.
Alternatively, just stick with strings.
For this specific case only (comparing how precise was the entered pi), use a std::string rather than a double. If someone who knows a couple of thousands of digits uses your program, the double-precision floating point variable will not be precise enough for a comparison. You will have to store a reference pi as a string too.
Incidentally, with this approach you will no longer have the issue of remembering how many decimal places were entered.
You might want to investigate the differences between decimal and binary representations of numbers.
Your user enters a decimal representation, i.e. each symbol represents a digit from 0 to 9.
On the other hand, a double is a binary representation.
Some numbers can be expressed with a decimal representation but have no equivalent in binary representation. For instance you cannot express 0.3 with a finite number of binary symbols.
That's why you should keep your input as decimal, for instance by using a string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My WIN32 (C++) code has a UINT lets call it number.
The value of this UINT (or INT doesn't matter) start with a 0 and is recognized as an octal value. It's possible to use the standart operators and the value will keep the octal-system. The same is possible with hex (with foregoing 0x).
The problem is I have to use the Value of number in a buffer to calculate with it without changing the value of number. I can assign a value like 07777 to buffer on declaration line but if use an operation like buffer = number the value in buffer is recognized on decimal base.
Anybody has a solution for me?
There's no such thing in C as an "octal value". Integers are stored in binary.
For example, these three constants:
10
012
0xA
all have exactly the same type and value. They're just different notations -- and the difference exists only in your source code, not at run time. Assigning an octal constant to a variable doesn't make the variable octal.
For example, this:
int n = 012;
stores the value ten in n. You can print that value in any of several formats:
printf("%d\n", n);
printf("0%o\n", n);
printf("0x%x\n", n);
In all three cases, the stored value is converted to a human-readable sequence of characters, in decimal, octal, or hexadecimal.
Anybody has a solution for me?
No, because there is no actual problem.
(Credit goes to juanchopanza for mentioning this in a comment.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For an assignment I have to create a "rock paper scissor lizard spock" game where I have to output the percentage of the winnings at the end. In the program I calculate the number of games player A and B have won including tie games, the total games played, and the win percentage of each player. All the variables are int except for the percentage which is a double variable type. When I calculate the percentage (games won / total games) I get a 0 as a result. Any ideas why? Sorry I could not provide any code, this is an assignment and I'm not allowed to post it anywhere online.
Without seeing code I can't be 100% sure, but my guess is that you're dividing values like this:
int numWins = /* ... */
int numLosses = /* ... */
double ratio = numWins / numLosses; // <-- Error!
In C and C++, that last line is a logic error. Although you're storing the result as a double, because you're dividing two ints the division is done as integer division and then stored in a double. C and C++ don't look at the type of the variable you're assigning to when deciding what division to use; the ratio of two ints is always computed using integer division.
To fix this, you can add a cast:
double ratio = (double)numWins / numLosses;
This casts numWins to a double, which ensures that the division is done as floating-point division because at least one of the operands is a double. That should fix your issue.
Hope this helps!
In most programming languages, division between two integers is an integer. Thus, 5 / 10 will give 0, as will 1 / 10 and 9 / 10. This takes place before assignment to the result variable, so the fact that the result variable is a double is irrelevant. You need to turn either the divisor or the dividend into a double for the floating point calculation to kick in.