Re: Floating Point and C casts? (was: Re: typecast internals)
From: Hoff Hoffman (hoff_at_hp.nospam)
Date: 05/04/04
- Next message: Rich Jordan: "Re: VMS opportunities HP misses every day"
- Previous message: Andrew Harrison SUNUK Consultancy: "Re: How to turn linux into VMS - memory refresher for Dave ..."
- In reply to: Niklaus: "typecast internals"
- Next in thread: Bob Koehler: "Re: typecast internals"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 04 May 2004 16:13:55 GMT
In article <d06ac2eb.0405032206.4ea0a009@posting.google.com>, niklaus@gamebox.net (Niklaus) writes:
: I would like to know more about casts.What exactly happens
: when casts are applied to a variable.I know that if an
: object of type int is applied an cast of float the result
: would be of type float.
You are mixing a discussion of integer and floating point storage
with a discussion of language constructs.
What's up? What particular data conversion problem are you really
looking to solve? Error messages? Conversion requirements, etc?
There are specific data format conversion routines available, for
instance -- casts are fairly restrictive in their capabilities,
but the implementation is a component of the language definition.
Conversion routines provide additional flexibility.
What problem are you seeking to solve?
: What i would like to know is the about the
: internals when a cast is applied ? Say we have
:
: int i = 3;
: double j;
: j = (double) i;
:
: What happens in the above statement ? Can someone
: explain me at bit level or a considerable explantion ?
The integer is converted from a signed integer into to a double float
value, typically into an F-, G-, or T-format floating point value with
the target depending on the OpenVMS platform and compilation options.
If you really want to know what happens at the bit level, well, dig up
the assembly documentation for whichever platform is in use and ask the
compiler to generate machine code listings. Then look at the hardware
instructions and the run-time library calls used for the conversion.
Such hardware assembly language and Run-Time Library documentation is
available of course, and is referenced in the OpenVMS FAQ.
:2)
: Also i would like to know what happens say
:
: i = (int)j;
:
: Again an explanation at the bit level would be very helpful.
: If we have
j is converted from a double float to an integer.
Note that fractions have ranges of integers that can be represented,
as calculated by the bits in the exponent and the fraction. There
are a number of values which have no direct corrolation when format
conversions are involved, or when the results of an operation might
not be what you expected due to differences in routing.
Monetary values, for instance, are and typically should be stored in
integer cells, though most programmers might initially think to use
a floating point value. (Most programmers make that mistake once. :-)
:3)
:
: float f = 4.3;
: int i;
: i = (int) f;
:
: What happens here ? How does the truncation take place ?
The fraction is not part of nor representable as an integer, so
it is removed.
:4)
: Is it similar to how int got promoted to double in the question 2.
C has specific and documented rules for casting and has specific details
on what is called "promotion", though details of the conversions are also
specific to the underlying floating point implementation provided by the
hardware.
IEEE is probably the most portable and common floating point format, and
is available in hardware on Alpha and I64. VAX has floating point formats
which are very close to IEEE (with VAX predating the IEEE floating point
formats), but these formats are not typically implemented off the VAX
and the Alpha platforms. OpenVMS I64 provides various capabilities within
the compilers and the run-time specifically for VAX floating point formats
and operations, and specifically for compatibility.
IEEE floating point conversions are detailed in the compiler manuals
and (obviously) in the IEEE documentation.
D is VAX single-precision and F and G are VAX floating point doubles,
while S and T are the IEEE single- and double-precision floating point
values, respectively.
:5)
: How are side effects defined ? When do
: i say typecasting is an side effect ?
casting is used when the data types do not match and you need to force
the match. The compiler has generated a warning explaining the error,
and you are providing specific additional instructions to the compiler.
In some cases, the conversion can be performed with no data loss, while
in others there will be a loss of precision and/or truncation.
:6)
: Is truncation a side effect of type casting or not ?
Um, what else would you expect the compiler to do? :-)
Again, the real question is: "what are you up to?" Pointed questions
can and usually will get you answers -- look at the compiler-generated
machine code listings being the most detailed answer to the questions
here -- but answers such as this might not be the best answer available
to the question you had intended to ask.
---------------------------- #include <rtfaq.h> -----------------------------
For additional, please see the OpenVMS FAQ -- www.hp.com/go/openvms/faq
--------------------------- pure personal opinion ---------------------------
Hoff (Stephen) Hoffman OpenVMS Engineering hoff[at]hp.com
- Next message: Rich Jordan: "Re: VMS opportunities HP misses every day"
- Previous message: Andrew Harrison SUNUK Consultancy: "Re: How to turn linux into VMS - memory refresher for Dave ..."
- In reply to: Niklaus: "typecast internals"
- Next in thread: Bob Koehler: "Re: typecast internals"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|