473,382 Members | 1,086 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 numbers

I know it'll print the 2nd one bcause of the imprecision of the floating point number representation. what is the change if I want to solve this problem, could somone explain it to me?
---
double x = 9.5 * 0.1;
double y = 0.95;
if (x = = y)
printf("They are equal\n");
else
printf("They ar not equal.\n");

---
Nov 28 '08 #1
5 1419
boxfish
469 Expert 256MB
You could multiply them both by 100 and round them to the nearest integer, but what exactly is the problem you are trying to solve?
Nov 28 '08 #2
vmpstr
63
Normally, you would do something like:

Expand|Select|Wrap|Line Numbers
  1. #define EPSILON 1e-6
  2.  
  3. if(fabs(x - y) < EPSILON) printf("They are equal\n");
  4. else printf("They are not equal\n");
  5.  
Nov 28 '08 #3
Ganon11
3,652 Expert 2GB
Of course, you could make a simple function to do this:

Expand|Select|Wrap|Line Numbers
  1. const double EPSILON = 1e-6;
  2.  
  3. int fEquals(double x1, double x2) {
  4.    return fabs(x1 - x2) < EPSILON;
  5. }
  6.  
  7. // ...
  8.  
  9. double x = 9.5 * 0.1;
  10. double y = 0.95;
  11. if (fEquals(x, y))
  12.    printf("They are equal\n");
  13. else
  14.    printf("They ar not equal.\n");
Nov 28 '08 #4
JosAH
11,448 Expert 8TB
Better use the relative difference.

kind regards,

Jos
Nov 28 '08 #5
thank you everyone, I've a much clearer idea now.
Nov 29 '08 #6

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

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
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....
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: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I have been unable to get it to work perfectly due to...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
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 );...
11
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.