?
Solved

Is it guaranteed by modern C++ compilers that a caste from bool to double will always result only in 0.0 or 1.0?

Posted on 2011-09-30
6
Medium Priority
?
393 Views
Last Modified: 2012-05-12
double d;
bool b;
int i;

b = true:
d = (double) b;

// how about
i = 2;
b = (bool) i;
d = (double) b;
     
0
Comment
Question by:47pitch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 100 total points
ID: 36894671


see here:
http://msdn.microsoft.com/en-us/library/aa325135%28v=vs.71%29.aspx

public: static double ToDouble(
 bool value
);

The number 1 if value is true; otherwise, 0.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894682

http://bytes.com/topic/c/answers/674697-bool-int-cast
From bool to int, false is always 0, true is always 1.
From int to bool, 0 is always false, anything other than 0 is true.


// how about
i = 2;
b = (bool) i;
d = (double) b;

therefore d should become 1.0

as b becomes true
and then it is cast to double as 1.0


0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894863
I actually tried this, using gcc compiler on solaris
and got expected result : 1.000

I think such things should be fairly standardized
 

#include <stdio.h>
#include <stdbool.h>

void main(){
int i;

double d;
bool b;
i =2;
b = (bool) i;
d = (double) b;
printf("%3f\n",d);

}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 36895037


Compiler implicitly do the casting for you.

But better to use static_cast instead of simple c style casting.
Ex:
b = static_cast<bool>(i);
d = static_cast<double>(b);
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 100 total points
ID: 36895047
>> I think such things should be fairly standardized

Actually, the result is very well defined as far as the standard is concerned...

4.9 Floating-integral conversions

An rvalue of an integer type or of an enumeration type can be converted to an rvalue of a floating point
type. The result is exact if possible. Otherwise, it is an implementation-defined choice of either the next
lower or higher representable value. [Note: loss of precision occurs if the integral value cannot be represented
exactly as a value of the floating type. ] If the source type is bool, the value false is converted to
zero and the value true is converted to one.
0
 

Author Closing Comment

by:47pitch
ID: 36895115
Thanks!
I was looking for authoritative reference to show my colleagues
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question