Maths & Rounding

  • Thread starter Thread starter Daisy
  • Start date Start date
D

Daisy

Something simple, I'm sure. My code:

System.Windows.Forms.MessageBox.Show(((Int32)Math.Ceiling(1966 /
100)).ToString());

I'm expecting 20, but getting 19. What am I doing wrong?

Thanks
 
Hi Daisy,

I got the same results, even though 19.66 should raise to 20. (Maybe a
bug???) Anyhow, try this:

System.Windows.Forms.MessageBox.Show(int.Parse(Math.Round((decimal)1966
/100).ToString()).ToString());

HTH,

Bill P.
 
Imho it is right.

The way things are evaluated is:

1966 / 100 (int/int) = int

Math.Ceiling (result from above, being an int)

Cast to Int32

Convert to string.

The trick is that int / int = int is part of how maths works.

--
Regards

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Bill Priess said:
Hi Daisy,

I got the same results, even though 19.66 should raise to 20. (Maybe a
bug???) Anyhow, try this:

System.Windows.Forms.MessageBox.Show(int.Parse(Math.Round((decimal)1966
/100).ToString()).ToString());

I shortened it to:

System.Windows.Forms.MessageBox.Show(Math.Round((decimal)1966 /
100).ToString());

And it worked too. I'd just done it by casting both sides to Single, which
worked. If just one side (assuming cast is done before division?) works,
that's probably better :o)
 
Mark said:
I think you missed the point of integer division

an integer divided by an integer to give another integer doesnt round up or
down. Integers have no concept of decimal places and do not hold them.

Your code to divide 1966 by 100 would result in an integer holding 19, the
.66 would just be dropped as there was no memory reserved to hold this part.

Hope i shed a bit of light

Yeah, I didn't realise it would return an Int. Changing it to

((Single)1966 / 100)

returns a single, as I need :o)
 
This is pretty obvious, as I said. All operations are done in the highhest
precision present.

int / int = precision int

float / int = precision float.

I can only suggest you read the part about maths in the C# language
specification again.

--
Regards

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Something simple, I'm sure. My code:

System.Windows.Forms.MessageBox.Show(((Int32)Math.Ceiling(1966 /
100)).ToString());

I'm expecting 20, but getting 19. What am I doing wrong?

Nobody suggested the obvious. If you want to round, why not
(year+50)/100

/steveA


my email (e-mail address removed) is encrypted with ROT13 (www.rot13.org)
 
Thomas Tomicek said:
This is pretty obvious, as I said. All operations are done in the highhest
precision present.

int / int = precision int

float / int = precision float.

I can only suggest you read the part about maths in the C# language
specification again.

Yeah thanks, because sarcasm and smart-arséyness is the way you learnt too?

It's not "pretty obvious" to anybody that's never come across it before. I
started programming in Perl, PHP and VBScript, so it's not something I've
noticed. I'm quite suprised to see MVP after your name if this is the
attitude you usually post with. Whatever happened to the "No such thing as a
silly question, only a silly answer" way that *most* of the helpful people
in this group (and indeed this thread) seem to have?

</rant>
 
Thomas Tomicek said:
too?

No, i read it in the language specification for mathematical operations,
instead of trial and error.

I was refering to the language, not what I was posting about ;P


I

Then read the language specification before you start using the language.
This is similar to the C / C++ way of handling things, and it is fully
documented in language specs.

I don't know about you, but I have better things to do than RTFM front to
back. I've done enough programming to know how maths works, so it would have
been rather a waste of time I could've been coding in. And since there are
so many helpful people in groups like this, I decided to use the time
coding.

And FYI, when I got the wrong result, I went straight to the docs, and start
looking for an explination. I couldn't find one, which is why I posted here.
I tried Ceiling and other methods to no avail.
Well, it got readjusted by the RTFM attitude.

Only in some people :P

You may notice that I thoroughly explained WHY the problem occured, too.

Yes, you did. Unfortunately it was preceeded by "This is pretty obvious".

Sorry for not knowing C# inside out - believe it or not, that's because I'm
just starting to learn it... I do believe there's a gap between not knowing
a language and knowing it, and that's called learning.

Read the langauge specs, and you dont have to rant.

Nor would I have to if you left the replying to the other helpful people
around here that posted, or at least left out the patronising sarcasm. Not
something I've seen (or expected) from an MVP before. Thanks.
 
Daisy said:
I don't know about you, but I have better things to do than RTFM front to
back. I've done enough programming to know how maths works, so it would have
been rather a waste of time I could've been coding in. And since there are
so many helpful people in groups like this, I decided to use the time
coding.

What a sweet arrogance. "You have done enough programming to know how math
works", though this specs are clearly labelled and follow the behavior of VB
(if I remember right), definitly Java, C, C++.
And FYI, when I got the wrong result, I went straight to the docs, and start
looking for an explination. I couldn't find one, which is why I posted here.
I tried Ceiling and other methods to no avail.

Sure :-) Not to nag it, but you never read the language specs :-)
Yes, you did. Unfortunately it was preceeded by "This is pretty obvious".

As it IS obvious.
Sorry for not knowing C# inside out - believe it or not, that's because I'm
just starting to learn it... I do believe there's a gap between not knowing
a language and knowing it, and that's called learning.

"Enough programming not to read the langauge", and never have used any of
the of the "mainstream" langauges (C/C++/java/pascal) etc. that have the
same behavior :-) Yes, I see.
Nor would I have to if you left the replying to the other helpful people
around here that posted, or at least left out the patronising sarcasm. Not
something I've seen (or expected) from an MVP before. Thanks.

Well, I assume they knew me when they made me MVP. Can also be that my
answers are usually right and in enough detail to make people start
thinking.

--
Regards

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Well, I have to admit that I agree with Daisy. I have personally seen many
posts from her and although some of them are no brainers, for a learner,
she is usually well on track.

Not everyone here is a MVP, MCSE, MCSD. Not everyone has dealt with C/C++
or any other mainstream languages aside from MS ones.

As for the specs... most of us who have dealt with any of the mainstream
languages that MS has taken and bastardized (no offense, MSFT, but that is
usually the case) know that the specs are not always clearly written. For
instance, Math.Ceiling does not accept an int as a parameter *according to
the documenation*, but will take and implicitly convert an int to a double
in order to return a double. So, where in the documentation is that
mentioned? A learner who reads MSDN front to back is not going to find that
information easily, wouldn't you agree? In fact, to a learner,
Math.Ceiling(180/10) should return 2, but in fact, it doesn't. But,
according to the documentation posted here, it *should*.

As a matter of fact, here is the documentation from MSDN 2003 (April) which
does not show that an implicit conversion is taking place.


<paste>
..NET Framework Class Library

Math.Ceiling Method [C#]See Also
Math Class | Math Members | System Namespace | Round | Floor Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server
family
Language
C#

C++

JScript

Visual Basic

Show All
Returns the smallest whole number greater than or equal to the specified
number.

[Visual Basic]
Public Shared Function Ceiling( _
ByVal a As Double _
) As Double
[C#]
public static double Ceiling(
double a
);
[C++]
public: static double Ceiling(
double a
);
[JScript]
public static function Ceiling(
a : double
) : double;
Parameters
a A number. Return Value
The smallest whole number greater than or equal to a. If a is equal to NaN,
NegativeInfinity, or PositiveInfinity, that value is returned.

Remarks
The behavior of this method follows IEEE Standard 754, section 4. This kind
of rounding is sometimes called rounding toward positive infinity.

Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server
family

See Also
Math Class | Math Members | System Namespace | Round | Floor


----------------------------------------------------------------------------
----

Send comments on this topic.

© 2001 Microsoft Corporation. All rights reserved. </paste>


Now, with that posted, where does RTFM come into play?

Bill P.
 
Bill Priess said:
Well, I have to admit that I agree with Daisy. I have personally seen many
posts from her and although some of them are no brainers, for a learner,
she is usually well on track.

Not everyone here is a MVP, MCSE, MCSD. Not everyone has dealt with C/C++
or any other mainstream languages aside from MS ones.

Not everyone claims he knows the language without reading the specs.
As for the specs... most of us who have dealt with any of the mainstream
languages that MS has taken and bastardized (no offense, MSFT, but that is
usually the case) know that the specs are not always clearly written. For
instance, Math.Ceiling does not accept an int as a parameter *according to
the documenation*, but will take and implicitly convert an int to a double
in order to return a double. So, where in the documentation is that
mentioned? A learner who reads MSDN front to back is not going to find
that

It is properly documented in a lot of places. Look at the specifications on
the implicit transformations of valuue types. This is beginners section.

Just look at the documentation, in this case the C# Programmer's reference:

ms-help://MS.NETFrameworkSDKv1.1/csref/html/vclrfimplicitnumericconversion.h
tm

It lists ALL implicit conversions.

Or, if you prefer the language specification:

ms-help://MS.NETFrameworkSDKv1.1/csspec/html/vclrfcsharpspec_6_1_2.htm

The whole chapter 6.1 of the language specifications lists all Implicit
conversions.

Thinking about them is your job.

Putting it mildly - I am happy you arew not a lawyer or a doctor, as not
making your homework would put you into jail pretty fast in these
businesses. If you loose in court, saaying "I wa just trying and never read
the law" is not a good excuse.
information easily, wouldn't you agree? In fact, to a learner,

I dont. I am a SELF-LEARNER. In contray to you, though, I propably took the
two hours to read the langauge specifications FIRST.
Math.Ceiling(180/10) should return 2, but in fact, it doesn't. But,
according to the documentation posted here, it *should*.

No, it should NOT. This is pretty clear to anyone reading the math
documentation AND knowing the basics of how functions are evaluated.
As a matter of fact, here is the documentation from MSDN 2003 (April) which
does not show that an implicit conversion is taking place.
....snipped...

Because it is not part of the method. It is part o the LANGUAGE, and it is
VERY properly documented. Links above. Referring to the method documentation
just shows you have a little brutally said a total disrespect for the
language. It is pretty clear that the INPUT into ceiling used is ONE
float/int/whatever, it is also clear that the mathematical expression is
evaluated FIRST.

NOW - this evaluation follows the rules laid out in.

You may want to try to understand sections:

7.2.6 Numeric promotions
and
7.4.2 Overload resolution

In combination with
7.2.1 Operator precedence and ssociativity
and
2.4.4.2 Integer literals

it is clear that
* 180/10 is evaluated first.
* 180 and 10 are integer literals
* 180 / 10 is performed as a division integer to integer

NOW - the documentation states (7.7.2, btw - Division Operator):

"The division rounds the result towards zero, and the absolute value of the
result is the largest possible integer that is less than the absolute value
of the quotient of the two operands"

So the output of 180 / 10 is clearly defined to be an integer, including the
rounding rules, as both inputs are integers. And THIS is what you put into
the Math.Ceiling method.

Surely all this is not documented in the Math.Ceiling method - it is of no
business to the method. It is basic arithmetic operations performed by the
langauge before the method sees the result of the operation.
Now, with that posted, where does RTFM come into play?

At the moment you learn the LANGUAGE and how it resolves the arithmetic
operation whose output you put into the method you took the freedom to
quote.

Just shows that when you dont know the language you want to program in, the
documentation of the library is of no use, and that no ibrary documentation
saves you from learning the langauge first. References are given through my
whole post, referring to the language specification, which is part of the
MSDN documentation OR can be found in the Framework documentation (less
weighty) under

..NET Framework SDK / Reference / Compiler and Languag Reference / C# / C#
Language Specification

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Bill Priess said:
Well, I have to admit that I agree with Daisy. I have personally seen many
posts from her and although some of them are no brainers, for a learner,
she is usually well on track.

Not everyone here is a MVP, MCSE, MCSD. Not everyone has dealt with C/C++
or any other mainstream languages aside from MS ones.
<snip>

Thank you Bill :)
I wasn't going to reply to Thomas, already having the answer, but seeing
such a snotty reply from an MVP suprised me :o(
 
Nor did I. I claimed to know enough to get me going. Argue with that if
you
wish, but I'm on my way through writing a newsreader, including a custom
control (listview/trreeview thing, custom painting, etc.), lots of stuff
I've never done before. I'd just never come across int/int=int before. I
guess you must've skipped the learning stage and just suddenly knew
everything overnight. Oh, if only I was as arrog^h^h^h^h^hclever as you.
^^^ lmfao Daisy... ;)

As for the MVP... he may be one hell of good coder, but his people skills
are way below normal. Upon speaking to a few other MVPs/MCSDs yesterday, it
seems that a lot of people would assume what we did. go figure... guess
they must be beginners too... ;)

<thread finish="true" />
 
As for the MVP... he may be one hell of good coder, but his people skills
are way below normal. Upon speaking to a few other MVPs/MCSDs yesterday, it
seems that a lot of people would assume what we did. go figure... guess
they must be beginners too... ;)

I always say 95% of hte people working in IT are only one thing:
incompetent. Reading was once considered to be part of the job - times seem
to change. Sadly, the problems dont.

--
Regards

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Back
Top