I am using the boost::spirit parser. At one point when I use real_p, the value coming out of the parser stack is 38672000 instead of the actual value, 386731500. Some how it is considering it as a float value, I think. Is there anyway to fix this? Do I need to set the precision of real_p, or am using real_p in the wrong context?
Related
In a C++ wxWidgets GUI, I am trying to implement a way for the user to change the value of an optimization problem parameter. The value has only meaningful impact on the problem if it changes by an order of magnitude. So the most convenient way to do it would be to display the current value in E notation (e.g., 1e-3) in a spin control and use an increment that is an order of magnitude, so that each click on the up or down arrow increases or decreases the exponent by one.
I am struggling to figure out how to do that. The wxSpinCtrlDouble requires a constant increment, as far as I can tell, so I cannot write something like "times 10".
But I feel like this is a common use case, so there should be a simple way to do it. Can anyone nudge me in the right direction?
There is no built-in way to do it, other than the obvious one: use wxSpinCtrl for just the exponent and a separate control (or maybe even a static 1) for the mantissa.
I receive a JSON Array from a server which looks like: [0.00015099, 1, -672.41163]
These values are orderbook entries. If I try to parse all values as double, my price differs slightly from the price in the JSON array. It is clear for me, that this happens because of the double conversion, but how to program around some crazy stuff like this?
Need these values also for calculation, compares etc.
I am using Qt5 and C++.
Any hint?
Well, you should use some Decimal type if you want to deal with money to be on a safe side. Unfortunately, Qt does not have decimal type for some reason. So, you may end up with some rounding rules/conventions if you have no other choice. Otherwise you'd better to implement your own implementation or use some existing solution like qdecimal.
I have been using C++ for quite some time by now and literally took things for granted.
Recently, I asked myself how can the compiler return accurate values{always} when I use out of range values for calculation.
I understand the 2^n{n = bits} concept.
For example: If I would like to add two int's which are out of range such as:
10e6, I would expect the compiler to return a result that is wrong as the bits are overwritten and ultimately represent a wrong integer. But this is never seen to happen.
Can anyone shed some light over this.
Thanks.
One common question ,may be I am wrong at this point but last two hours on google not give me any answer,
Question is how to convert CString to float without using std: library in c++.
I tried with wcstod() function but, This function has very different behaviour.
Please find following code,
CString Tempconvert = "32.001";
double TempConvertedValue = wcstod(Tempconvert,NULL);
So, Finally TempConvertedValue should have value 32.001. But i am getting TempConvertedValue = 32.000999999999998
Please provide me any help which convert
String "32.001" to float 32.001 only. Without using std: library.
What you see is an example of floating point internal representation. You can have a look here to see how double are stored in memory. See also this question to know why debugger shows you this value.
With this storing format, it is not possible to represent a decimal number exactly. That is why money calculations usually use special decimal types that are either custom or part of the programming language.
i have written a program and it works with 3D coordinates (i.e. x,y,z).
input data for my program was like
50903.85 21274.97 15.03
50903.57 21274.96 15.08
50903.33 21274.95 15.17
and i got the output with some more columns. So, i got the same x,y,z for my output file.
50903.85 21274.97 15.03
50903.57 21274.96 15.08
50903.33 21274.95 15.17
so, my program works properly, i guess.
then, i used another data set, having more digits than the previous data,
512330.98 5403752.71 330.39
512331.01 5403754.18 329.44
512331.06 5403755.59 329.56
and my output was like;
512331 5.40375e+006 330.39
512331 5.40375e+006 329.44
512331 5.40376e+006 329.56
here, i am not able to get real values. and x values are also rounded. i cant think what should be the reason?
in my program, i used "double" for assigning variables for x,y,z values. SO, i would like to know what is the maximum numerical value that can be refereed to double?
if someone need to work with very long values, what should be the relevant variable?
Numbers aren't changing, you are just seeing a different notation change there. Perhaps you are using something other than %f to format your doubles? See printf format parameters.
Better yet, check out this StackOverflow question: How to avoid scientific notation for large numbers?
Those numbers, such as 5.40375e+006, are another way of representing doubles. When they get sufficiently large, they are by default printed in scientific notation. 5.40375e+006 is 5.40375 * 10^6, or 5403750.
Have a look at this http://www.cplusplus.com/reference/clibrary/cfloat/
Doubles have about 16 decimals of accuracy (source), so you shouldn't have any problem here, unless you're printing floats.