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

G

Greenhorn

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.
 
J

jacob navia

Greenhorn said:
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
 
G

Greenhorn

float k = 12.4;

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

greenhorn
 
J

jacob navia

Greenhorn said:
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.
 
I

infobahn

jacob said:
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.
 
F

Flash Gordon

jacob said:
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.
 
G

Greenhorn

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.
 
O

osmium

Greenhorn said:
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.
 
G

Greenhorn

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
 
M

Mark McIntyre

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.
 
L

Lawrence Kirby

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
 
L

Lawrence Kirby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,421
Latest member
StacyTaver

Latest Threads

Top