473,508 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clarify the difference in representation of numbers

48 New Member
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.   float me = 0.7;
  4. if(me<0.7)
  5. printf("C");
  6. else
  7. printf("C++");
  8. }
For the above code my compiler prints as C

However for the code
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.   float me = 1.7;
  4. if(me<1.7)
  5. printf("C");
  6. else
  7. printf("C++");
  8.  
my compile prints as C++.

Hence kindly clarify why is the above difference?
Aug 29 '10 #1
4 1467
whodgson
542 Contributor
and where do you change the values of me? ......to trigger the print op?
Aug 30 '10 #2
vensriram
48 New Member
I am not changing the value of me anywhere.
In the first program the if statement compares whether me(i.e.0.7) is lesser than 0.7 and the above decision turns true.

In my next code the value of me is 1.7 and when i compared me(i.e. 1.7) with 1.7 the decision shows that me is greater than 1.7.

At first i though me is float and if we speify a number with decimal point explicitly compiler will treat that as double and hence in the first case me<0.7. But this doesn't works for my second code. Hence kindly clarify
Aug 30 '10 #3
donbock
2,426 Recognized Expert Top Contributor
Take a look at what every computer scientist should know about floating point.

When assigning a value to me, the value is effectively cast to a float. When comparing me to a value, me is effectively cast to a double. When does changing from one floating point type to another change the value? It depends on implementation-dependent features of the underlying representation.
Aug 30 '10 #4
haicp
4 New Member
In this kind of comaprisions, 99% of the time the else part of the if condition gets executed.

In this example, when you try to compare 0.7 with 0.7, what actually happens internally is, one of the 0.7 is interpreted as 0.6999999999.

Floating point representation is machine dependent.

Even though there is not much of a difference between 0.7 and 0.6999999999, the if condition fails.

You execute the same code on another machine, probably you will get another result.

So, never use floating numbers for comparision.
Sep 3 '10 #5

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

Similar topics

8
5739
by: Benedikt Wismans | last post by:
dear group! <script language="javascript"> var a = 60381.11; var b = 1437261.58; var c = a + b; alert (c); -> 1497642.6900000002 var a = 60381.11; var b = 1437261.50;
10
2661
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
19
605
by: alain dhaene | last post by:
Hi, I have a form with a input field. A user has to give a number greater than 0. I have build a validation that alerts the user when he gives a double, or a number less than 0. But I have...
7
41318
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
10
15612
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
10
2827
by: ben | last post by:
i'm learning about the floating point format that's used on my computer (apple mac so powerpc) i've written a function to print out the bits in a float to see how floats are represented and i...
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
64
3856
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
15
3723
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
6
4630
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
0
7224
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
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
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,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
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...
1
5049
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...
0
4706
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...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.