473,654 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ 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 5033
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2267
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 an interesting solution (no, really!) which, in the cold light of morning, still seems pretty cool. The gist is this: Constants would be allowed to
8
2118
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
2357
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
13722
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
7040
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 give them a default value through some XML shaping? Thanks.
10
23592
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 FOO_STORE to be, say -10, I get a warning about unsigned comparison and I'm seeing an infinite loop. If I do initialize FOO_STORE = -10, I don't see any warnings. No infinite loop.
4
10849
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
1
2031
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 ButtonPress, but I don't see them in any of the Tkinter source files. Are these just magic values that come out of the TK side of things and are not defined in Python? Code like this makes me think I'm doing something wrong: if event.type == 2:...
6
2088
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){...
43
3199
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 I create two independent instances of this class; the code is as follows.
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.