If you have two overloaded functions:
double maximum(double a, double b, double c);
int maximum(int a, int b, int c);
They work fine if you call maximum() with all arguments as doubles, or
all as ints. But, if you mix up doubles with ints in the argument
list, it doesn't know which maximum() to call... but only one could
possibly match -- the one that takes doubles.
I assume this is not a bug, and is by definition... but, why?
Jason
Jul 26 '07
15 2040
I am not sure in what sense it's a loss of information, actually.
You can't be serious? If I convert a floating point value of pi to an
integer, and get 3, then I've lost the fractional part -- that's the
information that's lost. I don't think anyone would disagree that
information has been lost. It's no different from converting a 32-bit
integer to a byte, you may lose information, since the upper 24-bits
may have had 1's in them that are now no longer stored. That's a lose
of information.
The
conversion is well-defined, it discards the fractional part and retains
the integral part, in the same fashion as when you divide two integers
the result is [essentially] rounded to the next integer toward zero.
The conversion is well defined. That doesn't imply information is not
lost.
Is that a loss of information? <shrug>
Yes, of course, it is.
Jason
It's a serious bug in the standard.
Agreed.
There have been several
proposals to "deprecate" such implicit conversions (one by
Stroustrup himself, I think), but for whatever reasons, they
never got anywhere.
Wow.
Jason
Jason Doucette wrote:
>It's worse than that. The conversion from double to int discards the fractional part, but if the resulting integer is outside the bounds of int, it's undefined behaviour.
Are you positive about that? I thought it set it to the maximum /
minimum integer value... In my programs, if there's a case a floating
point will be out of integer range, I check before the cast, just in
case. I don't think I've ever tested what would happen if I didn't...
The quote from the standard (§4.8):
An rvalue of a floating point type can be converted to an rvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded. The behaviour is undefined if the truncated value cannot be
represented in the destination type.
--
rbh
Jason Doucette wrote:
>I am not sure in what sense it's a loss of information, actually.
You can't be serious? If I convert a floating point value of pi to an
integer, and get 3, then I've lost the fractional part
I think you confuse the word "lost" with the word "discarded" . The
former implies the unwanted result due to having no control over the
outcome of some events.
I believe most of the complaints about the language "inadequaci es" stem
from the inability of some people to take responsibility to learn and
consciously use (or not use) certain features.
-- that's the
information that's lost. I don't think anyone would disagree that
information has been lost.
*I* disagree. So you think wrong.
It's no different from converting a 32-bit
integer to a byte, you may lose information, since the upper 24-bits
may have had 1's in them that are now no longer stored. That's a lose
of information.
No, it's not.
>The conversion is well-defined, it discards the fractional part and retains the integral part, in the same fashion as when you divide two integers the result is [essentially] rounded to the next integer toward zero.
The conversion is well defined. That doesn't imply information is not
lost.
Yes, it does. If you know what the conversion does and you don't want
to "lose" the information, [read my lips:] do not use the conversion.
>Is that a loss of information? <shrug>
Yes, of course, it is.
<shrugWhateve r.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Jul 27, 10:49 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
wrote:
Jason Doucette wrote:
It's worse than that. The conversion from double to int discards the
fractional part, but if the resulting integer is outside the bounds of
int, it's undefined behaviour.
Are you positive about that? I thought it set it to the maximum /
minimum integer value... In my programs, if there's a case a floating
point will be out of integer range, I check before the cast, just in
case. I don't think I've ever tested what would happen if I didn't...
The quote from the standard (§4.8):
An rvalue of a floating point type can be converted to an rvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded. The behaviour is undefined if the truncated value cannot be
represented in the destination type.
Thanks, Robert! Can't be more clear than that...
Jason
I think you confuse the word "lost" with the word "discarded" . The
former implies the unwanted result due to having no control over the
outcome of some events.
I think this is being pedantic... what the writers mean by "lost" is
that the information has disappeared, and you cannot get it back.
It's unfortunate that programmers don't use proper English, since it
happens all the time, and most arguments are over definitions and not
an actual problem...
(...it is possible to lose something, but still have control over the
outcome of events, if you're not careful... perhaps that's why they
use that term? It's hard to say.)
I believe most of the complaints about the language "inadequaci es" stem
from the inability of some people to take responsibility to learn and
consciously use (or not use) certain features.
I generally agree, but sometimes I think it's nice that the language
protects you as well -- for instance, some languages use the alternate
slash for integer division (and complains if you use the regular one,
which is reserved for floating point), which *forces* you to realize
what you are doing, and that the result will be an integer, and not a
floating point value... and then you don't get bit. Even I, after
many years of programming C, have been bitten by integer division,
since it's not something I use often, and some languages give the
result as floating point, which I have assumed at times...
Jason This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roy Yao |
last post by:
Why the following code let my compiler complain an overloaded function
Init()?
// code begin
template<class T> class BicircularList
{
template<class T> class Iterator;
template<class T> class ChainNode
{
friend Iterator<T>;
|
by: PengYu.UT |
last post by:
Hi,
I run into error with the following program. Would you please help me?
Best wishes,
Peng
struct tag1{};
struct tag2{};
|
by: Vish |
last post by:
Hi all,
I am having a build error in one of the overloaded functions in my class.
The function takes either a string as a parameter or a type referenced in
another dll as a parameter. My class references the dll where the type of the
parameter is defined. I am able to create to create my class in a form
without any build errors and without any references to the DLL where the
other parameter type is defined. The form only uses the...
|
by: siegfried |
last post by:
The std::lower_bound function requires a function pointer as its last argument. This is simple if you don't overload.
How do I I call std::lower_bound with the last argument a function pointer when I need to pass a certain instance of an overloaded function?
Thanks,
Siegfried
|
by: rolandz |
last post by:
Hi,
Maybe somebody has been fighting with the problem that I do, currently.
I have a class that has method f(). The two versions of the f() method
accept different objects: Int and Short. These objects have
constructors that allow implicit conversions from simple types. All
this has been defined as follows:
<code>
class Int
| |
by: c1t1z3n |
last post by:
hiya, i'm having this weird error on my project. As far as I know "ambiguous call to overloaded function" should only occur when the compiler must choose from several methods, but here i don't think it's the case. I've got the following class:
class item
{
public:
item(string id_,string datatype_);
//item(string id_,string datatype_,int periodo_);
//item(string id_,string datatype_,string headerfile_);
//item(string id_, string...
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
void print(char c)
{
cout << "from print(char c) : " << c << endl;
return;
|
by: Noah Roberts |
last post by:
I am trying to keep a vector of tuples sorted based on the first member
and so for my insert function I am calling std::lower_bound with a bound
comparator like the following:
std::lower_bound(sit, eit, t, boost::bind(tuple_t::get<0>(), _1) <
boost::bind(tuple_t::get<0>(), _2));
The problem is that get<is overloaded via const. There are the two
definitions:
|
by: Joseph Turian |
last post by:
I have a templated class with the following methods:
Vocab(const T& t);
Vocab(unsigned uid);
However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?
Thanks,
|
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...
|
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,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |