DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. JV Guest

## Integer Division

How do I divide 2 integers in Visual C++ and have the division return a quotient
and a decimal instead of just an integer? For example, in the division of
22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
what data type would hold the return value? I'm trying to make an application
that computes the first thousand numbers of pi.

//cheesy example

int main()
{
double ans;
ans = 22/7; //it returns 3 instead of a decimal
return 0;
}

2. required field Guest

## Re: Integer Division

you can cast it as a float or double

ans = (float) 22/7

>
>How do I divide 2 integers in Visual C++ and have the division return a

quotient
>and a decimal instead of just an integer? For example, in the division of
>22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
>what data type would hold the return value? I'm trying to make an application
>that computes the first thousand numbers of pi.
>
> Any comments are greatly appreciated!
>
>//cheesy example
>
>int main()
>{
> double ans;
> ans = 22/7; //it returns 3 instead of a decimal
> return 0;
>}
>
>

3. Danny Kalev Guest

## Re: Integer Division

required field wrote:
>
> you can cast it as a float or double
>
> ans = (float) 22/7

actually, this won't do. The cast is applied to the result, which

ans = 22.0/7.0;

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221

>
> >
> >How do I divide 2 integers in Visual C++ and have the division return a

> quotient
> >and a decimal instead of just an integer? For example, in the division of
> >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1. Also,
> >what data type would hold the return value? I'm trying to make an application
> >that computes the first thousand numbers of pi.
> >
> > Any comments are greatly appreciated!
> >
> >//cheesy example
> >
> >int main()
> >{
> > double ans;
> > ans = 22/7; //it returns 3 instead of a decimal
> > return 0;
> >}
> >
> >

4. required field Guest

## Re: Integer Division

ans = (float) 22/7 ;

This works just fine in vc++ 6.0 std.
The (float) typecast has a higher precendence than '/'
so it really makes the 22 a float then does the division.

ans = (float) (22/7);
Then it would cast only the result.

Maybe its different in Borland??

Danny Kalev <dannykk@inter.net.il> wrote:
>
>
>required field wrote:
>>
>> you can cast it as a float or double
>>
>> ans = (float) 22/7

>
>actually, this won't do. The cast is applied to the result, which
>
> ans = 22.0/7.0;
>
>Danny Kalev
>
>"The ANSI/ISO C++ Professional Programmer's Handbook"
>http://www.amazon.com/exec/obidos/ASIN/0789720221
>
>
>
>
>>
>> >
>> >How do I divide 2 integers in Visual C++ and have the division return

a
>> quotient
>> >and a decimal instead of just an integer? For example, in the division

of
>> >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1.

Also,
>> >what data type would hold the return value? I'm trying to make an application
>> >that computes the first thousand numbers of pi.
>> >
>> > Any comments are greatly appreciated!
>> >
>> >//cheesy example
>> >
>> >int main()
>> >{
>> > double ans;
>> > ans = 22/7; //it returns 3 instead of a decimal
>> > return 0;
>> >}
>> >
>> >

5. Danny Kalev Guest

## Re: Integer Division

required field wrote:
>
> ans = (float) 22/7 ;
>
> This works just fine in vc++ 6.0 std.
> The (float) typecast has a higher precendence than '/'
> so it really makes the 22 a float then does the division.
>
> ans = (float) (22/7);
> Then it would cast only the result.
>
> Maybe its different in Borland??

Thanks for the info. Actually, I used a different compiler, which
probably has a bug regarding precedence (not Borland). So your code is
correct. However, I'd rather avoid casts in general (and C-style cast in
particular).

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221

>
> Danny Kalev <dannykk@inter.net.il> wrote:
> >
> >
> >required field wrote:
> >>
> >> you can cast it as a float or double
> >>
> >> ans = (float) 22/7

> >
> >actually, this won't do. The cast is applied to the result, which
> >
> >instead, use the following form:
> > ans = 22.0/7.0;
> >
> >Danny Kalev
> >
> >"The ANSI/ISO C++ Professional Programmer's Handbook"
> >http://www.amazon.com/exec/obidos/ASIN/0789720221
> >
> >
> >
> >
> >>
> >> >
> >> >How do I divide 2 integers in Visual C++ and have the division return

> a
> >> quotient
> >> >and a decimal instead of just an integer? For example, in the division

> of
> >> >22 by 7, I want a return of 3.14285... instead of just 3 remainder 1.

> Also,
> >> >what data type would hold the return value? I'm trying to make an application
> >> >that computes the first thousand numbers of pi.
> >> >
> >> > Any comments are greatly appreciated!
> >> >
> >> >//cheesy example
> >> >
> >> >int main()
> >> >{
> >> > double ans;
> >> > ans = 22/7; //it returns 3 instead of a decimal
> >> > return 0;
> >> >}
> >> >
> >> >

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise