NO

Author Topic: Few basic queries  (Read 2983 times)

phaedrus

  • Guest
Few basic queries
« on: January 27, 2016, 03:50:52 PM »
Hi all,
1)I am planning to use Pelles C to learn C.So I have a bunch of example programs I want to try out. I have created a project,and keep deleting the previous .C file and adding the new .C file,to compile and execute each example in turn.Is there a better way to do it?

2)This snippet :
Code: [Select]
e=(d*c);                      // 102   float arithmetic
printf("\n e is %f",e);
prints 101.99998,instead of 102.Is there any reason for this?

3) sizeof(int),sizeof(float) both give 4bytes.Is this OK ?I thought float would be 8bytes wide.
TIA.


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Few basic queries
« Reply #1 on: January 27, 2016, 11:09:52 PM »
Hi all,
1)I am planning to use Pelles C to learn C.So I have a bunch of example programs I want to try out. I have created a project,and keep deleting the previous .C file and adding the new .C file,to compile and execute each example in turn.Is there a better way to do it?
Well, maybe. If you are playing with a lot of small examples (about a screen page or less), than this is certainly one way to do it.
For longer things, and examples that contain more than one file, creating a new project for each example is IMHO the better way though...
Quote
2)This snippet :
Code: [Select]
e=(d*c);                      // 102   float arithmetic
printf("\n e is %f",e);
prints 101.99998,instead of 102.Is there any reason for this?
Yes. Actually there are even two reasons for that.
1) floating point arithmetic is always subject to inaccuracies. As we don't know in your snippet how c,d and e are defined and how d and c get their values, a deviation from the the resulting value that you assume is to be expected. That's true for pretty much any language/compiler that does not use BCD variables/arithmetic, as for most floating point values, there is no exact representation with the available bits for the mantissa.
2) You did not specify the output mask/accuracy for the %f specifier, hence the program will use the default value which seems to be the same as "%.5f", meaning 5 digits after the decimal point
Quote
3) sizeof(int),sizeof(float) both give 4bytes.Is this OK ?I thought float would be 8bytes wide.
Nope. Not sure what led you to assume this but float is synonym with single and is hence a 32bit value. To get a 64bit value, you have to explicitly use double...

Ralf

phaedrus

  • Guest
Re: Few basic queries
« Reply #2 on: January 28, 2016, 04:55:10 PM »
Ralf,thanks for replying.
Quote
As we don't know in your snippet how c,d and e are defined and how d and c get their values, a deviation from the the resulting value that you assume is to be expected.
e and d were defined as float.c is defined as int.

Other matters are pretty much clear from your reply.
Thanks again!