LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2010, 04:48 AM   #16
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301

yeah
 
Old 02-15-2010, 08:32 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dogs View Post
That's cool. I haven't been in a situation where I thought the L would be necessary.

so what you mean is -

1.3 is a float, whereas 1.3L is a double? (long float?)
I'm not sure your guess is correct.

A number like 1.3 is double. To make it float one needs to write it as 1.3f . Small 'l' means long, but it's an integer long. I am not sure what capital L means - there are long doubles in C99. You better check it - this all is documented.
 
Old 02-15-2010, 08:42 AM   #18
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I looked it up and it says: "Floating point constants are assumed to be double. During calculations, other types are promoted (float), then after calculation, if the answer is stored as float, it is demoted (can reduce speed). To override this, add 'f' or 'F' suffix, ex. 2.3F, or if you want long double use 'L', ex. 54.3L. Otherwise it is of type double."

Actually this is from my notes on C Primer book.
 
Old 02-15-2010, 09:14 AM   #19
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Yes, as you say, double is the default - here are the relevant quotes from sections 2.13.3 and 3.9.1 of the ISO C++ standard:

Quote:
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.
Quote:
The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.
If you work with (single precision) float, the compiler may or may not convert the intermediate results to double (it is neither required nor disallowed by the standard). In fact, you can get different results depending on whether optimization is turned on or not.

The GNU compiler implements the long double using the 80 bit floating point facility on the 32 bit x86 machines.
 
Old 02-16-2010, 09:55 AM   #20
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
I feel certain that after having read the comments in this thread, that I will always, from here on, be able to distinguish accurately between ints, floats, and doubles.


As a side note - Yesterday's class went pretty well. We technically discussed programming, if you consider discussing the failures of certain governmental programs to be forms of programming. Terrorism was mentioned, too. Looked at us as if he was thinking, "You never can tell... Any one of em could be, but statistically, all of them are out to get me!"

That's closer than we've been so far.
 
Old 02-16-2010, 01:19 PM   #21
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by Dogs View Post
Terrorism was mentioned, too. Looked at us as if he was thinking, "You never can tell... Any one of em could be, but statistically, all of them are out to get me!"
They have to drive it into you like some demon. They do this with other things too, in case you haven't noticed. It they say something enough you will believe it. These things that they push so hard, these things must be questioned the most, in fact they should be assumed to be false at first and later proven to be true or otherwise.
 
Old 02-16-2010, 08:24 PM   #22
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by Sergei Steshenko View Post
A number like 1.3 is double. To make it float one needs to write it as 1.3f.
Furthermore, the return type of atof() is double. And float gets promoted to double within varags. So double is really the "normal" floating-point type in C. Whoever named them screwed up: the 64-bit type should have been called "single precision" (so the 32-bit would be "half precision").

I disagree with an earlier poster that float is "totally useless" though. I used it last year to build millions-of-elements correlation matrices for the Netflix Prize. Had to save memory.
 
Old 02-16-2010, 10:04 PM   #23
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by Dogs View Post
Code:
   const float CONV_FACT_INCH_MILES = .000015782;//1/63360
   const float CONV_FACT_FOOT_MILES = .0001893;//1/5280
   const float CONV_FACT_YARD_MILES = .0005681;//1/1760
   const float CONV_FACT_MILES_MILES = 1;// one mile, for symmetry
   const float CONV_FACT_INCHES_METRIC = .0254;//1/39.37
   const float CONV_FACT_FEET_METRIC = .3048;//1/.3048
   const float CONV_FACT_YARDS_METRIC = .9144;//1/.9144
   const float CONV_FACT_MILES_METRIC = 1609.3000;//1/1609.3
All you need is the last 4 of those. If you want to convert a measurement to miles, just convert it to meters, then convert meters to miles.
 
Old 02-17-2010, 12:44 AM   #24
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
All you need is the last 4 of those. If you want to convert a measurement to miles, just convert it to meters, then convert meters to miles.
Losing precision at each step..
 
Old 02-17-2010, 12:48 AM   #25
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@Dan04

Quote:
When the PDP-11 computer arrived at Bell Labs, Dennis Ritchie built on B to create a new language called C ...
http://www.livinginternet.com/i/iw_unix_c.htm

Quote:
16-bit PDP-11 appeared somewhere in the beginning of 1970's.
http://www.alasir.com/articles/alpha_history/index.html

 
Old 02-17-2010, 04:48 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dogs View Post
I feel certain that after having read the comments in this thread, that I will always, from here on, be able to distinguish accurately between ints, floats, and doubles.
...
Just remember not to trust us and to recheck what we are saying in the official documentation - I am serious.
 
1 members found this post helpful.
Old 02-17-2010, 05:28 AM   #27
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by tuxdev View Post
Losing precision at each step..
Indeed you would, you should also take into account sig figs and rounding errors. But heck for programming class you probably don't care and neither does the teacher.
 
Old 02-17-2010, 12:22 PM   #28
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
I find truly, the computer is only accurate to about 2 decimal places after the point.
 
0 members found this post helpful.
Old 02-17-2010, 06:32 PM   #29
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by tuxdev View Post
Losing precision at each step..
But, it all conversions are written in terms of a common base unit, there are only two steps: Multiply to convert from the source unit to the base unit, and divide to convert from the base unit to the destination unit.

Sure, this may mean getting 1 mile = 63360.00000000001 inches instead of 63360. But it has the huge advantage that you only have to write O(n) conversion factors instead of O(nē).
 
Old 02-17-2010, 06:47 PM   #30
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by smeezekitty View Post
I find truly, the computer is only accurate to about 2 decimal places after the point.
"float" (usually) has 24 significant bits. That's only 7.22 significant digits.

Suppose the value you're converting is between 65536 and 131072 meters (about 41-81 miles). Then you need 17 bits to store the integer portion of the meters, so that leaves you only 7 bits for the fractional part, giving you a resolution of 1/128 m.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gparted bootcd - I'm retarded Plastech Linux - Newbie 2 07-29-2007 02:40 PM
Who's socially retarded Garda General 21 08-25-2006 06:25 PM
Azureus retarded in unfathomable way lasalsademuerte Linux - Software 4 04-02-2006 10:53 AM
Help a retarded user computer_67 Mandriva 6 11-02-2003 05:04 PM
Retarded nonstandard card FallingFrog Linux - General 1 04-04-2001 06:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration