-
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.
Any comments are greatly appreciated!
//cheesy example
int main()
{
double ans;
ans = 22/7; //it returns 3 instead of a decimal
return 0;
}
-
Re: Integer Division
you can cast it as a float or double
ans = (float) 22/7
"JV" <gjvelez@earthlink.net> wrote:
>
>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;
>}
>
>
-
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
already truncated.
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
Please reply to the newsgroup.
>
> "JV" <gjvelez@earthlink.net> wrote:
> >
> >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;
> >}
> >
> >
-
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.
If it had been:
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
>already truncated.
>
>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
>
>Please reply to the newsgroup.
>
>
>
>>
>> "JV" <gjvelez@earthlink.net> wrote:
>> >
>> >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;
>> >}
>> >
>> >
-
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.
>
> If it had been:
> 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
Please reply to the newsgroup.
>
> 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
> >already truncated.
> >
> >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
> >
> >Please reply to the newsgroup.
> >
> >
> >
> >>
> >> "JV" <gjvelez@earthlink.net> wrote:
> >> >
> >> >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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks