Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 16-03-2007, 07:48 PM   #1 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 395
Unhappy Doubt in C

Dunno whether its right to post it here....

Anyway, this is my doubt:

i couldn't find this in Google.......

---> if we declare a float, like:

float a=2.7;

it gets stored in the memory as 2.700000 with specifically 6 digits only, after the decimal point. Why? (may be related to its format)
__________________
When Roger's at play, opponents pray..!!!!!
abhi_10_20 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 16-03-2007, 08:02 PM   #2 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

Quote:
Originally Posted by abhi_10_20
Dunno whether its right to post it here....

Anyway, this is my doubt:

i couldn't find this in Google.......

---> if we declare a float, like:

float a=2.7;

it gets stored in the memory as 2.700000 with specifically 6 digits only, after the decimal point. Why? (may be related to its format)
It's predefined in the compiler. This is probably the wrong sect. it shud go in s/w troubleshooting.
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Old 16-03-2007, 08:13 PM   #3 (permalink)
Security Exp
 
47shailesh's Avatar
 
Join Date: Apr 2006
Posts: 733
Default Re: Doubt in C

Yes by default it is upto 6 point decimal...

But if you want it to 3 point decimal than use %.3f in the print statement to get result in upto 3 place of decimal

e.g.

printf("salary is %.3f\n", salary );

For in depth Knowledge read Dennis Ritche
__________________
We Love Once, And When We do We do it Well
47shailesh is offline  
Old 16-03-2007, 08:50 PM   #4 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,453
Default Re: Doubt in C

U can set any precision u want! Check my code
Quote:
#include <iomanip.h>
#include <iostream.h>
int main()
{
int p;
cout<<"Mediator asked to enter precision u wanna set : "; cin>>p;
double x(3.14);
cout <<"x = "<<fixed <<setprecision(p)<<x<<'\n';
cout <<"x/3 = "<<fixed <<setprecision(p)<<(x/3)<<'\n';
return 0;
}
U can set any precision using setprecision! Don't try large numbers or ur terminal might hang! I tried "100000000000" as precision and had to kill the program then!!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 16-03-2007, 09:15 PM   #5 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 395
Default Re: Doubt in C

thanks for your replies......

but still not satisfied somewhat...
i think it goes with the storage space reserved........like float requires 4 bytes
in a standard turbo c compiler.....and we need to do some 'masala munch' with these 4 bytes.....
__________________
When Roger's at play, opponents pray..!!!!!

Last edited by abhi_10_20; 16-03-2007 at 09:39 PM.
abhi_10_20 is offline  
Old 17-03-2007, 11:58 AM   #6 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

Quote:
Originally Posted by mediator
U can set any precision u want! Check my code

U can set any precision using setprecision! Don't try large numbers or ur terminal might hang! I tried "100000000000" as precision and had to kill the program then!!
Buddy, abhi_10_20 is talking about C. Your code is C++!
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Old 17-03-2007, 02:14 PM   #7 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,453
Default Re: Doubt in C

^^But his precise problem is about float!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 17-03-2007, 02:16 PM   #8 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

Quote:
Originally Posted by mediator
^^But his precise problem is about float!
ok.
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Old 17-03-2007, 03:46 PM   #9 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 395
Default Re: Doubt in C

isn't setprecision() found in C ?
__________________
When Roger's at play, opponents pray..!!!!!
abhi_10_20 is offline  
Old 17-03-2007, 04:50 PM   #10 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,453
Default Re: Doubt in C

setprecision is found from library "iomanip.h" and it doesn't matter if its c or c++ afaik!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 17-03-2007, 06:38 PM   #11 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

If you want a detailed manual on programming in C, see snip

i have uploaded it.
__________
After giving the captcha code, wait 30 secs. goto the bottom. when the downlaod now button gets activated, click it to download. Don't use any d/w manager.
__________
Was it useful?
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e

Last edited by mehulved; 20-03-2007 at 11:22 PM. Reason: Automerged Doublepost
The Unknown is offline  
Old 17-03-2007, 09:19 PM   #12 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 395
Default Re: Doubt in C

another c book into my kitty........
man...i can start a big library with my pdf's....
__________________
When Roger's at play, opponents pray..!!!!!
abhi_10_20 is offline  
Old 17-03-2007, 09:33 PM   #13 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

Quote:
Originally Posted by abhi_10_20
another c book into my kitty........
man...i can start a big library with my pdf's....
Ha hA ha ha.
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Old 20-03-2007, 03:55 PM   #14 (permalink)
Security Exp
 
47shailesh's Avatar
 
Join Date: Apr 2006
Posts: 733
Arrow Re: Doubt in C

Quote:
Originally Posted by The Unknown
If you want a detailed manual on programming in C, see snip
Same book i was talkin about..

in the pdf mentiontioned page number 22 gives the idea about your problem
__________________
We Love Once, And When We do We do it Well

Last edited by mehulved; 20-03-2007 at 11:24 PM.
47shailesh is offline  
Old 20-03-2007, 04:11 PM   #15 (permalink)
gEttIn 0uT@@@ BOXX
 
abhi1301's Avatar
 
Join Date: Mar 2007
Location: in the webspace
Posts: 83
Default Re: Doubt in C

Quote:
Originally Posted by The Unknown
Ha hA ha ha.
Or u better start trusting the replies in here .. Shilesh is perfectly correct .. it's 6 point precision by default and u cannot do anything about it if u want the value just 1 or 2 places do it just like it's written .. change the precision of output wid
printf("salary is %.3f\n", salary );

being more clear this will print upto 3 places after decimal if u write
printf("salary is %.2f\n", salary );

this prints 2 values and so on ..
__________________
Koi CHAKAR NAHI HAI >> SAB JUGAAD HAI
abhi1301 is offline  
Old 20-03-2007, 10:03 PM   #16 (permalink)
Security Exp
 
47shailesh's Avatar
 
Join Date: Apr 2006
Posts: 733
Default Re: Doubt in C

Quote:
Originally Posted by The Unknown
snip
this book is "W. Kernighan and Dennis M. Ritchie" is it not a copyright voilation.. if so then remove the link please..
__________________
We Love Once, And When We do We do it Well

Last edited by mehulved; 20-03-2007 at 11:25 PM.
47shailesh is offline  
Old 21-03-2007, 02:01 PM   #17 (permalink)
Wise Old Owl
 
The Unknown's Avatar
 
Join Date: Nov 2006
Location: Pune, Maharashtra, India
Posts: 1,728
Default Re: Doubt in C

Quote:
Originally Posted by 47shailesh
this book is "W. Kernighan and Dennis M. Ritchie" is it not a copyright voilation.. if so then remove the link please..
I don't know. It was given to me by a frnd.
__________________
KDE on ArchLinux
PHP, MySQL, PostgreSQL, Linux, Apache; Message me to hire (freelancing only)
Explore Technology @ http://www.itech7.com
Cheap and Reliable VPS Hosting @ http://j.mp/arHk5e
The Unknown is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 02:51 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2