float

R

ramtin

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

Dombo

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.
 
T

Thomas Boell

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?
 
S

Saeed Amrollahi

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
 
I

Ian Collins

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++.
 
I

Ian Collins

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.
 
I

Ian Collins

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.
 
R

ramtin

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.

thanks of all . . .
 
S

SG

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
 
C

Christopher

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

I never really understood what advantage there would be in using C++/
CLI. If I want CLI, why wouldn't I just use C# instead? Maybe so I can
have pointers? OT anyway I suppose.
 
D

Dombo

Op 27-Oct-11 23:29, Christopher schreef:
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

I never really understood what advantage there would be in using C++/
CLI. If I want CLI, why wouldn't I just use C# instead?

+1
 
8

88888 Dihedral

float d1= 4/3 ; /* promote to inetger only in the right hand side result */

float d2= 4.0/3 ; /* promote to float */
float d3= 4/3.0 ;
float d4= 4.0/3.0;
 
R

red floyd

float d1= 4/3 ; /* promote to inetger only in the right hand side result */
No, that's integer arithmetic, result promoted to float.
float d2= 4.0/3 ; /* promote to float */
float d3= 4/3.0 ;
float d4= 4.0/3.0;

Technically, the first two are promote to double, truncate to float.
The third is already double, truncate to float.
 
8

88888 Dihedral

red floydæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ1時14分07秒寫é“:
No, that's integer arithmetic, result promoted to float.


Technically, the first two are promote to double, truncate to float.
The third is already double, truncate to float.
------------------------------------------------------
int x=3;
int y=4;

float d = x/y ;
float d= ((float)x)/y ;

/* whether the compiler knows x,y are constants up to now ? */
 
R

red floyd

red floydæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ1時14分07秒寫é“:
------------------------------------------------------
int x=3;
int y=4;

float d = x/y ;
float d= ((float)x)/y ;

/* whether the compiler knows x,y are constants up to now ? */

What's your point? 4.0 and 3.0 are *DOUBLE* constants.
 
I

Ian Collins

Is there any compelling reason to use float instead of double ? If
not, use double.

Is there any compelling reason not to include any context in your post?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top