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

Test float and double

Hi !
will you please explain the reasons
main()
{
float a=1.1;
double b=1.1;
if(a==b)
printf("I like you/n ");
else
printf("I hate you /n");
}

OUTPUT :
I hate you
main()
{
int a=1;
float b=1.0;
if(a==b)
printf("I like you/n ");
else
printf("I hate you /n");
}
OUTPUT :
I like you
main()
{
long double a=1.1;
double b=1.1;
if(a==b)
printf("I like you/n ");
else
printf("I hate you /n");
}
OUTPUT :
I like you
Dec 9 '07 #1
1 1975
weaknessforcats
9,208 Expert Mod 8TB
Read this and post again of you still have questions:
Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
Due to the automatic rounding, these operators may report a condition as true when the numbers
are only close in value.

Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

To compare two floating point numbers, you have to establish a sigma error tolerance.

Expand|Select|Wrap|Line Numbers
  1. if (fabs(i - j) < 0.000001) { ... // almost equal }
  2.  
----------------------------------------------------------------------

Also, be aware of this:
Expand|Select|Wrap|Line Numbers
  1. float var = 1.1;
  2.  
Symbolic constants like 1.1 are double. A double is larger than a float so your compiler is giving
you a warning about truncation and possible loss of data as the double is converted to a float.

You should create the constant as a float by coding:
Expand|Select|Wrap|Line Numbers
  1. float j = 1.1f;    /* or 1.1F */
  2.  
Further, float is a type developed in the years of computers with small memories.
Unless you can write a specific reason on paper as to why you need a float,
I recommend you use double throughout.
Dec 9 '07 #2

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

Similar topics

6
by: Nate Bargmann | last post by:
I am working on a function that takes degrees, minutes, seconds coordinates and converts them to decimal representation. Traditionally, in DMS notation the '-' sign, to indicate west longitude or...
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
6
by: trentdk | last post by:
I want to test which language (testing C and FORTRAN) would be faster with math calculations; one test with intergers, and another test with floats. What math formulas/functions would you guys use...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
5
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
3
by: tomekwr | last post by:
Hi, I had to write a class which would emulate FPU using normal fixed point operations, and to test it in some real application. I thought about some encoding programs but I don't have much...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: 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...

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.