delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/22/09:57:52

Date: Mon, 22 Jul 1996 13:27:20 -0300
Message-Id: <1.5.4.16.19960722105148.39073562@dmeasc.rc.ipt.br>
Mime-Version: 1.0
To: jlouwere AT galaxy DOT csc DOT calpoly DOT edu (Jan Louwerens), djgpp AT delorie DOT com
From: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>
Subject: Re: Float to integer conversion

At 22:43 21/07/96 GMT, Jan Louwerens wrote:
>David L Clayton (dclayton AT dircon DOT co DOT uk) wrote:
>: Could somebody please tell me the best way to change an integer number to a
>: float, and vice versa?
>
>just type cast it.
>
>int i = 4;
>float f = 6.0;

I do not want to play finicky here but for the sake of completeness, it
should be emphasized that a constant "6.0" in ISO (ANSI) C has type double
                                                                    ======
sometimes we find this kind of example even in books of good origin (K&R 2nd
Ed. inclusive).

But the correct would be to use 6.0f or 6.0F to really keep clear to the
compiler that what you intend is a float constant at all.

>
>float to int:
>i = (int)f;
>
>int to float:
>f = (float)i;

This last cast although it is healthier to keep for the sake of documenting
the code, it is redundant due the way the type promotions occur in C. You
can find a full discussion of it in you favorite C programming reference.

>
>JL
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak                      E-mail: csrabak AT ipt DOT br
DME/ASC                                   Phone: 55-11-268-35221Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax:   55-11-268-5996
Av. Prof. Almeida Prado, 532.  Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019