By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,419 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,419 IT Pros & Developers. It's quick & easy.

C# can't do fractions!

P: n/a
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
This seems really archaic...surely i'm doing it wrong and there's something
i'm missing!?
Nov 16 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
No, it needs a floating point operand before it
is willing to promote the other operand to floating
point and perform a floating point divide. This
has been the way C has worked for decades and
C++ for most of 2 decades.

What you call an incorrect result is the correct
result of performing an integer divide as called
for the C# language specification.
This seems really archaic...surely i'm doing it wrong and there's something
i'm missing!?


Perhaps you a missing a C# reference.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 16 '05 #2

P: n/a
"KimD" <Ki**@discussions.microsoft.com> a écrit dans le message de news:
3F**********************************@microsoft.com...
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
This seems really archaic...surely i'm doing it wrong and there's something i'm missing!?


This is akin to Delphi where we have the integer division operator (div) and
the fp division operator (/).

AFAIK, this is necessary because of implicit casting that may occur during
the calculation.

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 16 '05 #3

P: n/a

"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
This seems really archaic...surely i'm doing it wrong and there's
something
i'm missing!?


This is normal behavior for integer division, found in C, C++, Java and in
C#. I'm sure there are many other languages that share the same approach on
integer division. Any competently taught beginning programming class will
pound this concept into the heads of students. The result is determined by
the operand types, *not* by the type where the result is stored.
IOW, double res = 2/3; will not give you 0.6666..., but 0 in res as the
result is already determined by the division operation on two integer
literals.
If you divide one integer into another, the result will be an integer.
Casting one or the other operand to a real number type, as you've shown,
gets the other operand promoted to the "higher" type and preserves the
precision in the result.
It's *not* a C# thing!

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 16 '05 #4

P: n/a
I think the main problem in the assumption is that 4 / 3 as it reads means
the number 4 divided by the number 3.

The compiler doesn't have an understanding of a number in that way, but
needs to differentiate between whole integers and fractionally valued floats
(or doubles etc).

This is done by either an explicit cast
(float)4 / (float)3
or by giving the compiler a sign that the number is not a whole number
4.0 / 3.0

As everyone has already pointed out, this is a compiler design that has
existed since the beginning.
"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
This seems really archaic...surely i'm doing it wrong and there's
something
i'm missing!?

Nov 16 '05 #5

P: n/a
Sorry - didn't mean to offend anybody's sensibilities! I've been programming
with Delphi for some time and this was never an issue and hence came as a
surprise. I suppose I just expected it to use the type of the result rather
than assume it to be an integer. Thanks for all the advice.
-Kim

"Dan Bass" wrote:
I think the main problem in the assumption is that 4 / 3 as it reads means
the number 4 divided by the number 3.

The compiler doesn't have an understanding of a number in that way, but
needs to differentiate between whole integers and fractionally valued floats
(or doubles etc).

This is done by either an explicit cast
(float)4 / (float)3
or by giving the compiler a sign that the number is not a whole number
4.0 / 3.0

As everyone has already pointed out, this is a compiler design that has
existed since the beginning.
"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0 etc.
C# needs explicit statement of numerator and denominator as float/double
etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
This seems really archaic...surely i'm doing it wrong and there's
something
i'm missing!?


Nov 16 '05 #6

P: n/a
That's the point... integer IS the type of the result. It's not that the
compiler assumes a return type, it's just that the / operator is defined on
types to return the same type as the inputs, in the same way as
multiplication, addition and subtraction are. And we've all been caught out
by this at some time or another :-)

Steve

"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
Sorry - didn't mean to offend anybody's sensibilities! I've been
programming
with Delphi for some time and this was never an issue and hence came as a
surprise. I suppose I just expected it to use the type of the result
rather
than assume it to be an integer. Thanks for all the advice.
-Kim

"Dan Bass" wrote:
I think the main problem in the assumption is that 4 / 3 as it reads
means
the number 4 divided by the number 3.

The compiler doesn't have an understanding of a number in that way, but
needs to differentiate between whole integers and fractionally valued
floats
(or doubles etc).

This is done by either an explicit cast
(float)4 / (float)3
or by giving the compiler a sign that the number is not a whole number
4.0 / 3.0

As everyone has already pointed out, this is a compiler design that has
existed since the beginning.
"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
> Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0
> etc.
> C# needs explicit statement of numerator and denominator as
> float/double
> etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
> This seems really archaic...surely i'm doing it wrong and there's
> something
> i'm missing!?


Nov 16 '05 #7

P: n/a
and the problem is??
"Steve McLellan" <sjm AT fixerlabs DOT com> wrote in message
news:eR**************@TK2MSFTNGP12.phx.gbl...
That's the point... integer IS the type of the result. It's not that the
compiler assumes a return type, it's just that the / operator is defined on types to return the same type as the inputs, in the same way as
multiplication, addition and subtraction are. And we've all been caught out by this at some time or another :-)

Steve

"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
Sorry - didn't mean to offend anybody's sensibilities! I've been
programming
with Delphi for some time and this was never an issue and hence came as a surprise. I suppose I just expected it to use the type of the result
rather
than assume it to be an integer. Thanks for all the advice.
-Kim

"Dan Bass" wrote:
I think the main problem in the assumption is that 4 / 3 as it reads
means
the number 4 divided by the number 3.

The compiler doesn't have an understanding of a number in that way, but
needs to differentiate between whole integers and fractionally valued
floats
(or doubles etc).

This is done by either an explicit cast
(float)4 / (float)3
or by giving the compiler a sign that the number is not a whole number
4.0 / 3.0

As everyone has already pointed out, this is a compiler design that has
existed since the beginning.
"KimD" <Ki**@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
> Just made a startling discovery - C# interprets 4/3 as 1 and 2/3 as 0
> etc.
> C# needs explicit statement of numerator and denominator as
> float/double
> etc. e.g. 4d/3d or 4.0/3.0 to be interpreted correctly
> This seems really archaic...surely i'm doing it wrong and there's
> something
> i'm missing!?


Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.