473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

comparison of float variable with const value less than 1.

Here is a C pgm, which gives comparison of float variable with its own value, wrong result.
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {float a=0.7;
  3. if(a<0.7)
  4. printf("it is less");
  5. else
  6. printf("it is large");
  7. }
  8.  
Result is
It is less

if value assigned is 1.0,

result is
it is large
Oct 25 '07 #1
1 1780
bartonc
6,596 Expert 4TB
Here is a C pgm, which gives comparison of float variable with its own value, wrong result.

main()
{float a=0.7;
if(a<0.7)
printf("it is less");
else
printf("it is large");
}

Result is
It is less

if value assigned is 1.0,

result is
it is large
0.7 will typically be represented in the computer as
0.69999999999999996, so comparing to the constant might not work quite right.
Oct 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dave | last post by:
Hello all, I would like to solicit suggestions on how to most efficiently accomplish something. I have profiled the project I'm working on and have found that calls to fabs() are taking a very...
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
3
by: bofh1234 | last post by:
I am trying to write a function that returns a float. This is what the function should do: It takes the socket number and a variable name. The function looks up the variable in an array of...
8
by: Gernot Frisch | last post by:
Uhm... Is there any way of making float pos; a union of float x,y,z; somehow, so I can use: mything = mything.y;
4
by: john | last post by:
The code: #include <iostream> #include <limits> int main() { using namespace std;
4
by: Stef Mientki | last post by:
hello, I had a program that worked perfectly well. In this program modules were dynamically added, just by putting the file in a predefined directory. Now one of the interface mechanisms was...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.