473,472 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
11 5012
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

float k = 12.4;

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

greenhorn

Nov 14 '05 #3
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
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
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9
by: Chris Gonnerman | last post by:
A while back (a long while actually) I was a participant in a long argument over decimal and rational numbers, and their inclusion into the core of Python. Last night in a dream I came up with...
8
by: Vineet Jain | last post by:
The following does not work although it seems like something you should be able to do. def someFunction(option=Constants.DEFAULT_VALUE): Thanks, V
2
by: Serg | last post by:
Hi. I have a problem with deserialization decimal value. I have a simple class: public class A { public decimal d; } The serialization a value 0.00000001 is OK. The file is
10
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
5
5
by: Matt D | last post by:
I have a bunch of constants used in my web services that the client also needs to have. Is there a way to declare public properties that are part of a class or structure as constants or at least...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
1
by: Noah | last post by:
I'm trying to match against Event.type for KeyPress and ButtonPress. Currently I'm using integer constants (2 and 4). Are these constants defined anywhere? The docs talk about KeyPress and...
6
by: Jeff | last post by:
I'd like to have a default array as a function parameter. I can do this: function my_function($MY_ARRAY = array('1'=>'one')){... But not this: function my_function($MY_ARRAY = $_POST){...
10
43
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
15
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.