473,385 Members | 1,736 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,385 software developers and data experts.

#INFs and casts

Hi,

I have the following code:

#include <stdio.h>

int main(int argc, char **argv)
{
double d = 1.0e100;

float f = 1.0e100;

float f1 = d;

printf("%6.4f\n", static_cast<float>(d));

printf("%6.4f\n", (float)(d));

printf("%6.4f\n", f);

printf("%6.4f\n", f1);

return 0;
}

that prints

10000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0.0000
10000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0.0000
1.#INF
1.#INF

My assumption is, as I am casting the doubles to float, it should als
print 1.#INF, but not so. Why?

Thanks in advance,
Shoban Jayaraj
Jul 26 '08 #1
1 1555
On 2008-07-26 08:05:16 -0400, Shoban Jayaraj <sh****@gmail.comsaid:
>
that prints

10000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0.0000
10000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0.0000
1.#INF
1.#INF

My
>
assumption is, as I am casting the doubles to float, it should als
print 1.#INF, but not so. Why?
There are two things that interact to produce this result.

First, when you pass an argument of type float as one of the extra
arguments to a function that takes a variable-length argument list,
that argument is promoted to double.

Second, unless you store a value in a variable, the compiler is allowed
to handle it at higher precision than its type calls for. On x86, for
example, floats and doubles are generally handled as 80-bit values,
rather than the 64 bits that a double uses, or the 32 bits that a float
uses.

So when you pass (float)d to printf, the compiler doesn't have to
truncate the value to float; it can simply passes it as a double, and
it looks like that's what's happening here. Same thing for the one with
the static_cast.

When you pass arguments that were stored as floats they are promoted to
double. Both f and f1 have the value infinity, and when they get
promoted to double they remain infinity.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 26 '08 #2

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

Similar topics

4
by: Michael Wagner | last post by:
I do some Windows kernel programming, where what I need to pass to some Kernel call is "void* Context". Sometime later, I will get that Conext back. I want to pass a class pointer to this system...
3
by: Howard | last post by:
Hi, I am maintaining a lot of code that is rife with C-style casts. I've seen a lot of comments that one should not use C-style casts at all. But I'm wondering what harm there could be in...
31
by: Jacob | last post by:
It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e....
10
by: Ralf | last post by:
Regarding numerical types, in my view, casts fall in one of two categories: 1. Casts that change the value of an object 2. Casts that are actually redundant, but get rid of compiler/lint warnings...
24
by: roberts.noah | last post by:
Where is it in the standard that C style casts are labeled depricated? I read that on a lot of websites but I can't find it in the standard. I have BS ISO/IEC 14882:2003 (2nd ed) as published by...
28
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following...
13
by: dpbsmith.janissary.2006 | last post by:
I know C++ mostly from "learning by doing." My main reference is Stoustrup's book. I was puzzled by something in a colleague's code that looked like this: abc=BOOL(def) I asked him what that...
81
by: jacob navia | last post by:
Hi I am still writing my tutorial book about C. Here is the section about casts. I would be interested in your opinions about this. Some people have definite views about this subject ("never...
61
by: jacob navia | last post by:
Continuing the discussion about casts, I would like to know your opinions about the hairy subject of casts as lvalues, i.e. This will fail under lcc-win32, but MSVC and gcc will accept it. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.