473,594 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about excerpt from "Accelerate d C++"

On 6.2.3/115 of "Accelerate d C++", there's this function:

double average(const vector<double>& v)
{
return accumulate(v.be gin(), v.end(), 0.0) / v.size();
}

The author notes that it's important to use 0.0 in the function, as
accumulate uses the third argument's type as its return type.

Wouldn't it be better to explicity say
return accumulate(v.be gin(), v.end(), double(0)) / v.size();
Is there a chance that floats and doubles might get mixed up if you don't do
a cast?

Thanks,
Joe
Jul 22 '05 #1
3 1320
0.0 is double but double(0) is int casted to double..
furthermore, 0.0f is float..
But actually all are same..
compiler might generate same code, I suppose. :)

"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:I5******** @news.boeing.co m...
On 6.2.3/115 of "Accelerate d C++", there's this function:

double average(const vector<double>& v)
{
return accumulate(v.be gin(), v.end(), 0.0) / v.size();
}

The author notes that it's important to use 0.0 in the function, as
accumulate uses the third argument's type as its return type.

Wouldn't it be better to explicity say
return accumulate(v.be gin(), v.end(), double(0)) / v.size();
Is there a chance that floats and doubles might get mixed up if you don't do a cast?

Thanks,
Joe

Jul 22 '05 #2
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:I5******** @news.boeing.co m...
On 6.2.3/115 of "Accelerate d C++", there's this function:

double average(const vector<double>& v)
{
return accumulate(v.be gin(), v.end(), 0.0) / v.size();
}

The author notes that it's important to use 0.0 in the function, as
accumulate uses the third argument's type as its return type.

Wouldn't it be better to explicity say
return accumulate(v.be gin(), v.end(), double(0)) / v.size();


The effect is exactly the same, because floating-point literals have type
double if you don't explicitly say otherwise. In other words, 0.0 has type
double, as does double(0), and 0.0f has type float. See page 303.

Jul 22 '05 #3
BekTek wrote:
0.0 is double but double(0) is int casted to double..
furthermore, 0.0f is float..
But actually all are same..
compiler might generate same code, I suppose. :)
Doubles are different than floats, as far as I know.

I wasn't sure that 0.0 would automatically be a double.

"Joe Laughlin" <Jo************ ***@boeing.com> wrote in
message news:I5******** @news.boeing.co m...
On 6.2.3/115 of "Accelerate d C++", there's this function:

double average(const vector<double>& v)
{
return accumulate(v.be gin(), v.end(), 0.0) /
v.size(); }

The author notes that it's important to use 0.0 in the
function, as accumulate uses the third argument's type
as its return type.

Wouldn't it be better to explicity say
return accumulate(v.be gin(), v.end(), double(0)) /
v.size();
Is there a chance that floats and doubles might get
mixed up if you don't do a cast?

Thanks,
Joe


Jul 22 '05 #4

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

Similar topics

3
2354
by: Gerlow | last post by:
Hi When I am using a ASP SQL question to my Access 2000 db via IIS 5.0 and have the SELECT TOP 15 syntax in the SQL question I get 15 rows in my answer if a have many rows in the table, but if I don´t have many rows I get MORE ( > 15) rows in my answer? Why? Thanks!!!!!!
1
1545
by: Steve Jorgensen | last post by:
I know some of you here were following involved with, or annoyed with the volume of traffic in the recent thread I started called "Open Question...". I'm posting this message because, though I don't think anyone was terribly angry or inflamed, there are things I don't like about how I conducted myself in the discussion. I would like to apologize for... 1. Being excessively enthusiastic about the new methodology I'm playing with and...
1
1614
by: AmiciDan | last post by:
When I attempt to create a new project in Visual Studio .NET 2003, I do NOT have the option to create the following: Microsoft Office 2003 Projects What do I need to install or do to get this option as a project type? I have a Universal MSDN subscription and am using Visual Studio .NET 2003 Enterprise Edition. I attempted to re-run the setup, but I did not see "Microsoft Office 2003 Projects" as an option.
6
1601
by: Adam Smith | last post by:
I have posted this and similar questions repeatedly and can't even raise a single response. I am being led to believe that this then 'Must be a stupid question' although people say that there is no stupid question. Is that another for political correctness I am attempting an install of 7.4.3 on FreeBSD O/S 4.9, apparently remnants of 7.3.x are scattered around on the disk from (a) previous ports installation, causing mutex_lock/unlock,...
42
3424
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
4
1266
by: R.A.M. | last post by:
Hello, (Sorry for my English...) I am learning C# and I have a question: is it good practice to use "using", for example: using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); ... connection.Close();
1
1314
by: blangela | last post by:
I am currently teaching 2 part-time (one evening per week) C++ courses using the Deitel & Deitel "C++ How To Program" text. Each course is 13 weeks in length, including time for labs and exams. In earlier posts to this usenet site, people have recommended that I switch texts to "Accelerated C++" by Koenig and Moo. I have just recently received a desk copy of the text from the publisher and I do believe that the approach taken by the...
2
1611
by: mirandacascade | last post by:
I am prompted to make these inquiries after seeing the following link to ctypes: http://docs.python.org/lib/module-ctypes.html in which ctypes is described as a foreign function library. What is the definition of "foreign function library"? Is the concept different from a "package"? Is the concept different from a "module"?
16
2619
by: Michael Bell | last post by:
I did an the Open University course MT262. I got 3/4 the way through it and only really came unstuck at classes. Following the advice of many, on this list and elsewhere, and impressed by their statement that students were writing substantial programs within a day, I bought "Accelerated C++" by Koenig and Moo. I find I just can't get on with it. I have started at the beginning, and I just can't follow it. The first few chapters do...
0
7947
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
7880
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
8374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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...
0
6665
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
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
3868
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
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1217
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.