473,412 Members | 1,873 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,412 software developers and data experts.

storing a float value

I would like to know that why float datayppe never stores exact value that is assigned to them?
for eg-> A piece of code
main()
{
float a=.07;
if (a<0.7)
printf("mismatch");
else
printf("C is precise");
}
Output: mismatch
Oct 4 '07 #1
7 2283
debasisdas
8,127 Expert 4TB
Question moved to C Forum.
Oct 5 '07 #2
rhitam30111985
112 100+
I would like to know that why float datayppe never stores exact value that is assigned to them?
for eg-> A piece of code
main()
{
float a=.07;
if (a<0.7)
printf("mismatch");
else
printf("C is precise");
}
Output: mismatch
you have assigned 0.07 to a, and comparing with 0.7... obviously there is a mismatch
Oct 5 '07 #3
Meetee
931 Expert Mod 512MB
0.07<0.7, it's a normal logic. So if(a<0.7) fulfills the condition and output will be mismatch!!

Regards
Oct 5 '07 #4
mattmao
121 100+
I would like to know that why float datayppe never stores exact value that is assigned to them?
for eg-> A piece of code
main()
{
float a=.07;
if (a<0.7)
printf("mismatch");
else
printf("C is precise");
}
Output: mismatch
If you want to make your code "interactive" with the user, google for "c scanf"

You can change your code to:
Expand|Select|Wrap|Line Numbers
  1. main()
  2.    {
  3.          float a;
  4.  
  5.          printf("input a float value\n");
  6.          scanf("%f", &a);
  7.  
  8.          if (a<0.7)
  9.           printf("mismatch");
  10.           else
  11.            printf("C is precise");
  12.   }
Oct 5 '07 #5
you have assigned 0.07 to a, and comparing with 0.7... obviously there is a mismatch

actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.
Oct 5 '07 #6
Savage
1,764 Expert 1GB
actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.
You cannot compare floats with < operator,because of float point accuracy.Epsilon is your friend.See this ..

Savage
Oct 5 '07 #7
JosAH
11,448 Expert 8TB
actually i typed it wrongly, the value assigned to a is 0.7, then there is a mismatch. Kindly let me know.
What Every Computer Scientist Should Know About Floating-Point Arithmetic.

kind regards,

Jos
Oct 5 '07 #8

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

Similar topics

6
by: Steve | last post by:
Hi, I've developed a testing application that stores formatted results in a database. Recently it was requested that I add the ability to generate graphs from the raw, un formatted test results...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
1
by: veremue | last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these values as Strings. Here is my code which is resulting...
13
by: Josip | last post by:
I'm trying to limit a value stored by object (either int or float): class Limited(object): def __init__(self, value, min, max): self.min, self.max = min, max self.n = value def...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.