- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- IF condition for float
Operating System - Linux
1753822
Members
9512
Online
108805
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2006 01:34 PM
тАО01-11-2006 01:34 PM
IF condition for float
Hi guys, I been working with a prgram.. when I use if to compare two float they can seem to be proven even if they are indeed equal
prt of my program is below:
/*the value = value_now cant work even if it is true*/
/*DECLARATION*/
float value; /*VALUE*/
float step_size; /*STEP_SIZE*/
float v_start; /*START SCAN VALUE*/
float v_stop; /*STOP SCAN VALUE*/
float value_now; /*THE CURRENT VALUE FOR ADC*/
float test;
int skip; /*SKIP OF STEPS*/
int steps; /*ADC STEPS*/
int skip_limit; /*SKIP_LIMIT*/
int i,j; /*COUNTERS*/
int a_steps; /*ACTUAL STEPS REQUIRE TO FIND VALUE*/
int stop_flag; /*FLAG TO STOP ADC OPERATION*/
..........
while( stop_flag == 1){
value_now = value_now + step_size;
a_steps++;
printf("counter = %d, value = %f, value now = %f, test = %f\n ", j, value, value_now);
if(value = value_now){ /*PROBLEM*/
stop_flag = 0;
printf("final value = %f\n ", value_now);
break;
}
j++;
} /*END WHILE*/
hope u can help.. many thanks!
regards
Henry
prt of my program is below:
/*the value = value_now cant work even if it is true*/
/*DECLARATION*/
float value; /*VALUE*/
float step_size; /*STEP_SIZE*/
float v_start; /*START SCAN VALUE*/
float v_stop; /*STOP SCAN VALUE*/
float value_now; /*THE CURRENT VALUE FOR ADC*/
float test;
int skip; /*SKIP OF STEPS*/
int steps; /*ADC STEPS*/
int skip_limit; /*SKIP_LIMIT*/
int i,j; /*COUNTERS*/
int a_steps; /*ACTUAL STEPS REQUIRE TO FIND VALUE*/
int stop_flag; /*FLAG TO STOP ADC OPERATION*/
..........
while( stop_flag == 1){
value_now = value_now + step_size;
a_steps++;
printf("counter = %d, value = %f, value now = %f, test = %f\n ", j, value, value_now);
if(value = value_now){ /*PROBLEM*/
stop_flag = 0;
printf("final value = %f\n ", value_now);
break;
}
j++;
} /*END WHILE*/
hope u can help.. many thanks!
regards
Henry
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2006 02:44 PM
тАО01-11-2006 02:44 PM
Re: IF condition for float
I don't suppose you can show the output of the 'counter=' line at the cross-over point?
Also, you may want to throw some precision on those floats for the output to get a better idea of what's going on..
Also, you may want to throw some precision on those floats for the output to get a better idea of what's going on..
One long-haired git at your service...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2006 02:48 PM
тАО01-11-2006 02:48 PM
Re: IF condition for float
>>> if(value = value_now){ /*PROBLEM*/
That's indeed a problem.
This assigns the value_now to value.
In order to compare you need to use ==
if(value == value_now){ /*SMALLER PROBLEM*/
But you still have to be careful!
Not all values can be represented exactly with floating points.
So you really want to deal with a range.
Something along the lines of
if ((value_now - value) < step_size) { ...
You'll have to decided whether to pre-calc the difference and then compare, whether to do an absolute value function first and so on.
In your example is seems (untested) that you can just test for
if (value_now > value) { /* no problem */
Cheersm
Hein.
That's indeed a problem.
This assigns the value_now to value.
In order to compare you need to use ==
if(value == value_now){ /*SMALLER PROBLEM*/
But you still have to be careful!
Not all values can be represented exactly with floating points.
So you really want to deal with a range.
Something along the lines of
if ((value_now - value) < step_size) { ...
You'll have to decided whether to pre-calc the difference and then compare, whether to do an absolute value function first and so on.
In your example is seems (untested) that you can just test for
if (value_now > value) { /* no problem */
Cheersm
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-11-2006 02:49 PM
тАО01-11-2006 02:49 PM
Re: IF condition for float
Your actually have 2 problems. One is syntax. The other is in your logic.
if (value = value_now) /* assignment */
should be:
if (value == value_now) /* comparison */
However, there is still a problem. Floating point vales should never be compared for equality because of the lack of precision.
For example, consider 2 values, very nearly equal:
a = 1.999999
b = 2.000001
You most likely would like those to compare equally:
The trick is to make a "nearly_equal" function using the fabs() function.
#include
# define SMALL_DIFF 0.00001
int nearly_equal(double a, double b)
{
int close_enough;
close_enough = (fabs(a - b) <= SMALL_DIFF);
return(close_enough)
}
So now your test should become:
if (nearly_equal((double) value, (double) value_now))
{
stop_flag = 0;
....
....
}
Note that also did not initialize stop_flag to a value so that your initial comparison at the top of the while loop is bogus as well.
if (value = value_now) /* assignment */
should be:
if (value == value_now) /* comparison */
However, there is still a problem. Floating point vales should never be compared for equality because of the lack of precision.
For example, consider 2 values, very nearly equal:
a = 1.999999
b = 2.000001
You most likely would like those to compare equally:
The trick is to make a "nearly_equal" function using the fabs() function.
#include
# define SMALL_DIFF 0.00001
int nearly_equal(double a, double b)
{
int close_enough;
close_enough = (fabs(a - b) <= SMALL_DIFF);
return(close_enough)
}
So now your test should become:
if (nearly_equal((double) value, (double) value_now))
{
stop_flag = 0;
....
....
}
Note that also did not initialize stop_flag to a value so that your initial comparison at the top of the while loop is bogus as well.
If it ain't broke, I can fix that.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP