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
Knows some wx things
Knows some wx things
Posts: 33
Joined: Sat Oct 28, 2017 9:33 am

Float and three decimal places

Postby palikem » Sun Dec 03, 2017 1:47 pm

Hello.

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

User avatar
doublemax
Moderator
Moderator
Posts: 11536
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places

Postby doublemax » Sun Dec 03, 2017 2:59 pm

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
Knows some wx things
Knows some wx things
Posts: 33
Joined: Sat Oct 28, 2017 9:33 am

Re: Float and three decimal places

Postby palikem » Sun Dec 03, 2017 3:39 pm

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: 11536
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places

Postby doublemax » Sun Dec 03, 2017 4:24 pm

Sorry, i don't understand.

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

palikem
Knows some wx things
Knows some wx things
Posts: 33
Joined: Sat Oct 28, 2017 9:33 am

Re: Float and three decimal places

Postby palikem » Sun Dec 03, 2017 4:47 pm

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: 11536
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Float and three decimal places  [SOLVED]

Postby doublemax » Sun Dec 03, 2017 5:03 pm

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
Knows some wx things
Knows some wx things
Posts: 33
Joined: Sat Oct 28, 2017 9:33 am

Re: Float and three decimal places

Postby palikem » Sun Dec 03, 2017 5:16 pm

I had it as a plan B.

Thank you doublemax

User avatar
eranon
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Float and three decimal places

Postby eranon » Sun Dec 03, 2017 5:57 pm

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


Return to “C++ Development”

Who is online

Users browsing this forum: Bing [Bot] and 11 guests