473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

negation operator !

Question about c++ standard:
On which types negation operator (!) can act?
In particular can it act on double variables?

// Is the bellow code 'standard c++' or not?
#include <iostream>

using namespace std;

int main()
{
double x=4.8;
if(!x) cout<<x<<endl;

return 0;
}

--
jimmij
Dec 8 '06 #1
3 4627
"jimmij" <ji****@jj.jjwrote in message news:rh*************@jj.jj
Question about c++ standard:
On which types negation operator (!) can act?
In particular can it act on double variables?

// Is the bellow code 'standard c++' or not?
#include <iostream>

using namespace std;

int main()
{
double x=4.8;
if(!x) cout<<x<<endl;

return 0;
}

--
jimmij

! operates on boolean variables. However, by Section 3.9.1/8 of the
Standard,

"Integral and floating types are collectively called arithmetic types".

and by Section 4.12 of the Standard,

"An rvalue of arithmetic, enumeration, pointer, or pointer to member type
can be converted to an rvalue of type bool. A zero value, null pointer
value, or null member pointer value is converted to false; any other value
is converted to true."

Thus a double is convertable to bool and so if(!x) is legal.
--
John Carson
Dec 8 '06 #2
John Carson wrote:
Thus a double is convertable to bool and so if(!x) is legal.
However, is it not possible that your double variable could easily
contain some value such as .00000000001 and that !x would return false.
I think it is generally unsafe to do exact compares on floating point
variables. Even this if (x == 5)
--
Ivan
http://www.0x4849.net

Dec 9 '06 #3
"Ivan Novick" <iv**@0x4849.netwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com
John Carson wrote:
>Thus a double is convertable to bool and so if(!x) is legal.
However, is it not possible that your double variable could easily
contain some value such as .00000000001 and that !x would return
false.
With 16 bit doubles, .00000000001 is non-zero so converts to true, which
means that the negation of it converts to false. No surprises there.

However, if you run this code:

#include <iostream>
using namespace std;

int main()
{
double x = .00000000001;
while(x)
{
x *= x;
cout << "x is " << x << endl;
if(!x)
cout << "!x evaluates to true\n";
else
cout << "!x evaluates to false\n";
}

return 0;
}

then eventually x becomes zero and !x becomes true because of the
limitations of the representation of a double. This is in spite of the fact
that, in terms of the mathematics of real numbers, squaring a non-zero
number always produces a non-zero result.

An example of the reverse sort of error is the following:

double a = 5.7987982749832, b = 3.27498327984;
double x = (a+b)*(a+b) - (a*a + 2*a*b + b*b);
cout << "x is " << x << endl;
if(!x)
cout << "!x evaluates to true\n";
else
cout << "!x evaluates to false\n";

Here x is zero in terms of the mathematics of real numbers, but (running on
Windows) evaluates to non-zero in terms of the arithmetic of doubles.
I think it is generally unsafe to do exact compares on
floating point variables. Even this if (x == 5)
Floating point representations are in general imprecise and hence
comparisons must be done with caution. Neither "exact compares" nor "inexact
compares" are guaranteed to give the right answer. However, either one may
be appropriate in particular situations.

None of this affects the legality of using a double in a situation that
calls for a bool.

--
John Carson
Dec 9 '06 #4

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
17
by: joshc | last post by:
I searched through the newsgroup for this and found the answer but wanted to make sure because of something that came up. I want the absolute value of a 'short int' so to avoid the dangers of...
1
by: Heloise | last post by:
Hi all, I'm trying to write an overloaded operator for unary negation e.g., MyClass a(5); MyClass b(0); b = -a; Here is my class implementation:
6
by: George Sakkis | last post by:
It's always striked me as odd that you can express negation of a single character in regexps, but not any more complex expression. Is there a general way around this shortcoming ? Here's an example...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
13
by: technocraze | last post by:
Hi community experts, Environment MS Acess, visual basic May i knw whether is there a way to create a static negation sign (-) in a textbox? Is there a possiblity that this can be done using...
6
by: sk.rasheedfarhan | last post by:
Hi , I am using regular expression in C++ code, . Negation is not working in the down loaded code. matches all characters except "a", "b", and "c] So I am in dilemma can negation work in C++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...

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.