Hello.
There is a possibility that the float variable has three decimal places ?
Float and three decimal places Topic is solved
Re: Float and three decimal places
Please provide some more context. The internal representation of a float and how you display it are two separate things. You can limit the number of visible decimal places with a typical printf format string.
https://en.wikipedia.org/wiki/Printf_format_string
https://en.wikipedia.org/wiki/Printf_format_string
Use the source, Luke!
Re: Float and three decimal places
I need to compare them.
One is fixed and the other is the cycle for ().
While I have addressed it through pomocny int, it is unnecessarily complicated
One is fixed and the other is the cycle for ().
While I have addressed it through pomocny int, it is unnecessarily complicated
Re: Float and three decimal places
Sorry, i don't understand.
What exactly do you need to do? Can you give an example?
What exactly do you need to do? Can you give an example?
Use the source, Luke!
Re: Float and three decimal places
One is the logarithmic axis
Code: Select all
float a, b, c;
b = 1.0;
c = 0.1;
for(a=0.1; a<=100.0; a=a+c){
if(a == b){
b = b * 10.0;
c = c * 10.0;
}
//I am interested in the variable "a" which should be 0.1, 0.2, 0.3~1.0 1.0, 2.0, 3.0~10.0 10.0, 20.0, 30.0~100.0
}
Re: Float and three decimal places
Comparing float values for equality is tricky and should be avoided. In your case i'd just use a separate integer that determines when to change the "step" value.
Use the source, Luke!
Re: Float and three decimal places
I had it as a plan B.
Thank you doublemax
Thank you doublemax
- eranon
- Can't get richer than this
- Posts: 867
- Joined: Sun May 13, 2012 11:42 pm
- Location: France
- Contact:
Re: Float and three decimal places
Easiest way to compare two floats for "sure":
However, as doublemax stated, the internal representation of a float is variable, undefined and unpredictable (hardware, context dependent) and is not limited to three decimal places even when you represent it like this on screen through printf or its wxWidgets equivalent wxString::Format.
Code: Select all
bool compare_float(double x, double y)
{
// Easy way to compare two floating point numbers a safer way than using "=="
// IN : two floats to compare
// OUT : true:equals, false:differents
// REF: http://www.drdobbs.com/cpp/its-hard-to-compare-floating-point-numbe/240149806
// NB: more sophisticated way to consider if needed:
// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
if (isnan(y))
return !isnan(x);
return x < y;
}
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]