Float and three decimal places Topic is 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.
Post Reply
palikem
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

Float and three decimal places

Post by palikem »

Hello.

There is a possibility that the float variable has three decimal places ?
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places

Post by doublemax »

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
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

Re: Float and three decimal places

Post by palikem »

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
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places

Post by doublemax »

Sorry, i don't understand.

What exactly do you need to do? Can you give an example?
Use the source, Luke!
palikem
Experienced Solver
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

Re: Float and three decimal places

Post by palikem »

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
}
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places

Post by doublemax »

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
Experienced Solver
Posts: 69
Joined: Sat Oct 28, 2017 9:33 am
Location: Slovensko

Re: Float and three decimal places

Post by palikem »

I had it as a plan B.

Thank you doublemax
User avatar
eranon
Can't get richer than this
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

Post by eranon »

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"]
Post Reply