## Float and three decimal places [SOLVED]

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
palikem
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

### Float and three decimal places

Hello.

There is a possibility that the float variable has three decimal places ?

doublemax
Moderator
Posts: 12885
Joined: Fri Apr 21, 2006 8:03 pm
Location: \$FCE2

### 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
Use the source, Luke!

palikem
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

### 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

doublemax
Moderator
Posts: 12885
Joined: Fri Apr 21, 2006 8:03 pm
Location: \$FCE2

### Re: Float and three decimal places

Sorry, i don't understand.

What exactly do you need to do? Can you give an example?
Use the source, Luke!

palikem
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

### 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}`

doublemax
Moderator
Posts: 12885
Joined: Fri Apr 21, 2006 8:03 pm
Location: \$FCE2

### Re: Float and three decimal places  [SOLVED]

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!

palikem
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

### Re: Float and three decimal places

I had it as a plan B.

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":

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;}`

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.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]