🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #616

Re: Pasar de float a int

#616
From: Pascal Bourguignon
Date: Mon, 18 Dec 2006 03:01
41 lines
2637 bytes
rvm <rvm@agujero-negro.escomposlinux.org> writes:

> Estoy intentando hacer un programa en Qt (que supongo que ya sab�is que se
> programa en C++). M�s o menos me las apa�o usado sus widgets y sus funciones.
> Pero ahora me encuentro con algo muy elemental, pero que no s� muy bien c�mo se
> hace.
>
> �C�mo paso en C++ un n�mero float al entero m�s cercano?
>
> Estoy haciendo algo as�:
> int n = (int) variable_en _float; (cuyo valor por ejemplo es 80.99999)
> pero al final resulta que n es 80 y no 81.

man roundl
man floorl
man ceill
man truncl


> Y por otro lado, �c�mo divido dos enteros para que el resultado sea un float?
> Ahora hago as�:
> float n = (float) 4/3;

No estoy seguro de las prioridades. Escribir�a: float n = ((float)4)/3;


> Si no pongo lo de (float) me da un entero. �Es esta la forma correcta de
> hacerlo?

Si.  Es decir, si tienes n�meros literales, mejor asi: float n=4.0/3.0;
Si tienes variables enteras:  int i,j;  float n=((float)i)/j;


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

COMPONENT EQUIVALENCY NOTICE: The subatomic particles (electrons,
protons, etc.) comprising this product are exactly the same in every
measurable respect as those used in the products of other
manufacturers, and no claim to the contrary may legitimately be
expressed or implied.
Technical Details
Message-ID: <87vek9aov4.fsf@thalassa.informatimago.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
References: <ku9h54-maa.ln1@dominio.inventado>

Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,8afb083384966002
X-Google-Attributes: gidf86c8,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: Pascal Bourguignon <pjb@informatimago.com>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Pasar de float a int
Date: Mon, 18 Dec 2006 04:01:51 +0100
Organization: Informatimago
Lines: 41
Message-ID: <87vek9aov4.fsf@thalassa.informatimago.com>
References: <ku9h54-maa.ln1@dominio.inventado>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: individual.net KwgEIpDnvJEMShvax3BMzQYFuz/UQfEJdWaZxWbKG2qK4Ib2Yz
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA
      oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9
      033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac
      l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR
      mV+hO/VvFAAAAABJRU5ErkJggg==
X-Accept-Language:         fr, es, en
X-Disabled: X-No-Archive: no
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.91 (gnu/linux)
Cancel-Lock: sha1:RXebPlYwAfQq7Nrg7TUB2bPlhVs=