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

Floating Point Doubt

2
Hi...Have this doubt about floating points in C

Look at the two programs below.

1. main()
{ float a = 0.7;
if(a < 0.7)
printf("Less");
else
printf("More");
}


2. main()
{ float a = 0.7, b=0.7;
if(a < b)
printf("Less");
else
printf("More");
}

Now, the first program prints Less and the second one prints More. And in both the programs if the value of a is printed, we get 0.700000.
The same program behaves differently when we use the datatype double instead of float.

Can anyone tell me why?
--
pits
Oct 11 '07 #1
4 1082
Ganon11
3,652 Expert 2GB
Take a look at this article, which explains why this type of error occurs.
Oct 11 '07 #2
pits
2
Thanks! This link looks useful...
Oct 11 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You have fallen victim to a) arithmetic conversions and b) automatic rounding.

When you have:
{ float a = 0.7, b=0.7;
if(a < b)
the calculation is done as a float (7 signiticant digits) whereas this code:

{ float a = 0.7;
if(a < 0.7)
converts a to double since 0.7 is a double. The calculation is done as a double (15 significant digits) so your answer may vary from the float version.

Plus, operators like <, >, ==, !=, etc generally cannot be used with floating point. If you Google for Floating Point Arithmetic you will be inudated with material.

Also, this code:
float a = 0.7;
should produce a compiler warning about truncation and possible loss of data as that double (0.7) is jammed into the float.
Oct 11 '07 #4

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

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
2
by: Sooha Park Lee | last post by:
I just fount out that most of machines provide standard macros handling "floating-point comparison" such as "isgreater()", "isgreaterequal()"... Thx. -S
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
137
by: mathieu.dutour | last post by:
Dear all, I want to do multiprecision floating point, i.e. I want to go beyond single precision, double precision and have quadruple precision, octuple precision and the like, and possibly with...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.