By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,596 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

default type for constants like 12.5, 1e-3, float or double ?

P: n/a
Hi,
K&R says that "floating point constants contain a decimal point
(123.4) or an exponent (1e-2) or both; their type is double; unless
suffixed. The suffixes f or F indicate a float constant; l or L
indicate a long double."

the question is are these constants double by default unless suffixed
with f or F ?

Thanks in advance.

greenhorn.

Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Greenhorn wrote:
Hi,
K&R says that "floating point constants contain a decimal point
(123.4) or an exponent (1e-2) or both; their type is double; unless
suffixed. The suffixes f or F indicate a float constant; l or L
indicate a long double."

the question is are these constants double by default unless suffixed
with f or F ?


Yes, unsuffixed floating point constants are double by default
Nov 14 '05 #2

P: n/a

float k = 12.4;

will the above variable 'k' be type coerced to double ?

greenhorn

Nov 14 '05 #3

P: n/a
Greenhorn wrote:
float k = 12.4;

will the above variable 'k' be type coerced to double ?

greenhorn

No, k remains a float as declared.
The double value 12.4 will be casted to a float, then
assigned to k.

Nov 14 '05 #4

P: n/a
jacob navia wrote:

Greenhorn wrote:
float k = 12.4;

will the above variable 'k' be type coerced to double ?

greenhorn

No, k remains a float as declared.
The double value 12.4 will be casted to a float, then
assigned to k.


Not so. A cast is an explicit conversion. No such conversion is
specified in the code.

3.5.7 of my C89 draft says:

"The initializer for a scalar shall be a single expression,
optionally enclosed in braces. The initial value of the object is
that of the expression; the same type constraints and conversions as
for simple assignment apply."

3.3.16.1 says:

"In simple assignment ( = ), the value of the right operand is
converted to the type of the assignment expression and replaces the
value stored in the object designated by the left operand."

So there is no cast here, merely an implicit conversion.
Nov 14 '05 #5

P: n/a
hi

wat is the morale behind making these type values, 12.3, 1e-3 etc
as double by default rather than as float (which holder smaller values,
values with lower precision). Is it due to an impression that these
values would more often be used with larger values, values with higher
precision.

greenhorn.

Nov 14 '05 #6

P: n/a
"Greenhorn" writes:
wat is the morale behind making these type values, 12.3, 1e-3 etc
as double by default rather than as float (which holder smaller values,
values with lower precision). Is it due to an impression that these
values would more often be used with larger values, values with higher
precision.


I assume you mean rationale where you say morale. By the time C came into
being there was already a long history of trying to do useful work with
32-bit floating point types, which were a part of the IBM 360 architecture.
The results, in many fields, were dismal. So the natural tendency in favor
of the double type permeates the C world. Many people consider the float
only suitable for long term storage, such as in a file, or produced by a
data collection device which doesn't usually have the ability to capture a
great many useful bits.
Nov 14 '05 #7

P: n/a
jacob navia wrote:
Greenhorn wrote:
float k = 12.4;

will the above variable 'k' be type coerced to double ?

greenhorn

No, k remains a float as declared.
The double value 12.4 will be casted to a float, then
assigned to k.


It won't be cast (or casted), it will be converted. You've been here
long enough to know that a cast is an explicit operation.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8

P: n/a
Hi,

what is the reason behind considering these constants (12.4,
1e-4 etc) as double by default rather than as float. Is there an
impression in language designers mind that these floating point types
would more often be used for very large values, values requiring high
precision (thus making double as default).

greenhorn

Nov 14 '05 #9

P: n/a
On 5 Mar 2005 01:48:03 -0800, in comp.lang.c , "Greenhorn"
<te************@yahoo.com> wrote:
Hi,
K&R says that "floating point constants contain a decimal point
(123.4) or an exponent (1e-2) or both; their type is double; unless
suffixed. The suffixes f or F indicate a float constant; l or L
indicate a long double."

the question is are these constants double by default unless suffixed
with f or F ?


As the quote says, they're double unless suffixed by f,F,l or L.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #10

P: n/a
On Sat, 05 Mar 2005 10:54:06 +0100, jacob navia wrote:
Greenhorn wrote:
Hi,
K&R says that "floating point constants contain a decimal point
(123.4) or an exponent (1e-2) or both; their type is double; unless
suffixed. The suffixes f or F indicate a float constant; l or L
indicate a long double."

the question is are these constants double by default unless suffixed
with f or F ?


Yes, unsuffixed floating point constants are double by default


Unsuffixed floating point constants have double type, always. Contrast
that with integer constants whose type can depend on the value.

Lawrence

Nov 14 '05 #11

P: n/a
On Sat, 05 Mar 2005 05:03:54 -0800, Greenhorn wrote:
Hi,

what is the reason behind considering these constants (12.4,
1e-4 etc) as double by default rather than as float. Is there an
impression in language designers mind that these floating point types
would more often be used for very large values, values requiring high
precision (thus making double as default).


In K&R C all floating point arithmetic was performed as double. So any
float operand was promoted to double before the operation occurred, which
would have included any float constants. So it made sense for all floating
point constants to have double type (and there were no floating point
suffices). Standard C added float and long double arithmetic but
maintained constant compatibility with K&R C. It is still reasonable
because double tends to be the most commonly used floating point type.

Lawrence
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.