Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   float (http://www.velocityreviews.com/forums/t754977-float.html)

ramtin 10-17-2011 07:40 PM

float
 
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 !!!

Dombo 10-17-2011 07:47 PM

Re: float
 
Op 17-Oct-11 21:40, ramtin schreef:
> 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 !!!


That is because 4 and 3 are integers, so the result of the division will
be an integer as well, after that the integer result is converted to a
float. If you use 4.0/3.0 you should get something close to 1.3333.

Thomas Boell 10-17-2011 07:50 PM

Re: float
 
On Mon, 17 Oct 2011 12:40:40 -0700 (PDT)
ramtin <tom.t.2090@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 !!!


What language is it?


Saeed Amrollahi 10-17-2011 08:05 PM

Re: float
 
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;

Regards,
-- Saeed Amrollahi

Ian Collins 10-17-2011 08:19 PM

Re: float
 
On 10/18/11 08:40 AM, ramtin wrote:
> hi all ,
>
> #include "stdafx.h"
> #include<stdio.h>
> using namespace System;
>
> int main(array<System::String ^> ^args)


This looks like a language almost, but not entirely, unlike C++.

--
Ian Collins

Ian Collins 10-17-2011 08:23 PM

Re: float
 
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;

?

Either way, the rules of whatever language the OP is using may be different.

--
Ian Collins

Ian Collins 10-17-2011 08:35 PM

Re: float
 
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

ramtin 10-17-2011 08:49 PM

Re: float
 
On Oct 17, 1:35*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> 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


thanks of all . . .

SG 10-18-2011 12:34 PM

Re: float
 
On 18 Okt., 01:55, Paul wrote:
> Ian Collins wrote in message
> > [...] ramtin wrote:

>
> >> int main(array<System::String ^> *^args)

>
> > This looks like a language almost, but not entirely, unlike C++.

>
> That is the syntax use din MSVC++ when you create a windows form
> projec(C++), probably a .net thing.


I would guess that a large fraction of the competent regulars
(including Ian) already know exactly what it is: C++/CLI. And yes,
that's what you get if you ask Herb Sutter to make .NET easily
accessible from C++ by use of language extensions.

Off-topic: I makes me chuckle a bit to see almost the same syntax
being used for C++/CX where the CX language extensions are used to
deal with WinRT's ref-counting object system. I guess Microsoft lost a
bit of interest in .NET and C++/CLI since this seems to make the CLI
and CX extensions mutually exclusive. Btw, I can recommend Herb
Sutter's "modern C++" talk. It almost sounds like he's trying to
convert .NET developers. "Look, how easy it is to write modern and
safe C++ code!".

Cheers!
SG

Sjouke Burry 10-26-2011 11:13 PM

Re: float
 
Thomas Boell <tboell@domain.invalid> wrote in
news:20111017215005.66a8d93d@arcadia:

> On Mon, 17 Oct 2011 12:40:40 -0700 (PDT)
> ramtin <tom.t.2090@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

cut

(int(4/(int)3 equals 1.
so whats new today??


All times are GMT. The time now is 05:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.