Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10946
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: float |
| Date | 2011-10-18 09:35 +1300 |
| Message-ID | <9g3ht3FpouU4@mid.individual.net> (permalink) |
| References | <fa3c19f8-c2ed-4d12-b18e-e40b574cdd31@g16g2000yqi.googlegroups.com> <c8e5a21a-c456-44b7-823c-5d6a7f3ea76d@g25g2000yqh.googlegroups.com> <9g3h6iFpouU3@mid.individual.net> <XIGdnUIx58bFEgHTnZ2dnUVZ8hCdnZ2d@giganews.com> <Ieedndng-aNZDQHTnZ2dnUVZ8j-dnZ2d@giganews.com> |
On 10/18/11 09:31 AM, Leigh Johnston wrote:
> On 17/10/2011 21:25, Leigh Johnston wrote:
>> On 17/10/2011 21:23, Ian Collins wrote:
>>> On 10/18/11 09:05 AM, Saeed Amrollahi wrote:
>>>> On Oct 17, 10:40 pm, ramtin<tom.t.2...@gmail.com> wrote:
>>>>> hi all ,
>>>>>
>>>>> #include "stdafx.h"
>>>>> #include<stdio.h>
>>>>> using namespace System;
>>>>>
>>>>> int main(array<System::String ^> ^args)
>>>>> {
>>>>> float d = 4 / 3 ;
>>>>> printf("%f",d);
>>>>> return 0;
>>>>>
>>>>> }
>>>>>
>>>>> when you run this ,it will be 1 in d ,but it should be 1.3333 in it !!!
>>>>
>>>> Hi Ramtin
>>>> Besides what Dombo wrote you can use static_cast:
>>>> float d = static_cast<float>(4)/3;
>>>
>>> If the OP were using C++, wouldn't it be easier just to write
>>>
>>> float d = float(4) / 3;
>>
>> No it would be easier to just write
>>
>> float d = 4.0f / 3.0f;
>
> Or even:
>
> float d = 4.0 / 3.0;
>
> as the compiler will probably optimize the division into a constant
> assignment.
That was already posted (the "what Dombo wrote" referenced above).
My point was there is no need to use static_cast, a temporary will do.
If the '4' was an integer variable for example.
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
float ramtin <tom.t.2090@gmail.com> - 2011-10-17 12:40 -0700
Re: float Dombo <dombo@disposable.invalid> - 2011-10-17 21:47 +0200
Re: float Thomas Boell <tboell@domain.invalid> - 2011-10-17 21:50 +0200
Re: float Sjouke Burry <S@P.NL> - 2011-10-26 23:13 +0000
Re: float Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-10-17 13:05 -0700
Re: float Ian Collins <ian-news@hotmail.com> - 2011-10-18 09:23 +1300
Re: float Leigh Johnston <leigh@i42.co.uk> - 2011-10-17 21:25 +0100
Re: float Leigh Johnston <leigh@i42.co.uk> - 2011-10-17 21:31 +0100
Re: float Leigh Johnston <leigh@i42.co.uk> - 2011-10-17 21:33 +0100
Re: float Ian Collins <ian-news@hotmail.com> - 2011-10-18 09:35 +1300
Re: float ramtin <tom.t.2090@gmail.com> - 2011-10-17 13:49 -0700
Re: float Ian Collins <ian-news@hotmail.com> - 2011-10-18 09:19 +1300
Re: float "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-10-18 00:55 +0100
Re: float SG <s.gesemann@gmail.com> - 2011-10-18 05:34 -0700
Re: float Christopher <cpisz@austin.rr.com> - 2011-10-27 14:29 -0700
Re: float Dombo <dombo@disposable.invalid> - 2011-10-28 18:14 +0200
Re: float 88888 Dihedral <dihedral88888@googlemail.com> - 2011-10-28 21:06 -0700
Re: float red floyd <no.spam.here@its.invalid> - 2011-10-28 22:14 -0700
Re: float 88888 Dihedral <dihedral88888@googlemail.com> - 2011-10-28 23:27 -0700
Re: float red floyd <no.spam.here@its.invalid> - 2011-10-29 08:24 -0700
Re: float ptyxs <kerloch@gmail.com> - 2011-10-30 01:40 -0700
Re: float Ian Collins <ian-news@hotmail.com> - 2011-10-30 22:27 +1300
csiph-web