Hi there,
I believed that Math.Ceiling is like Math.Round except that it always rounds
up to the next
but this
double d = Math.Ceiling(29/15);
will get me "1" where I'd expect it to return "2". Am I on the wrong track?
Could somebody be so kind an shed some light?
Thanks in advance!
Matthias 5 11280
"matthias s" <postamt__add__ kieferfrost__do dd__dewrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
Hi there,
I believed that Math.Ceiling is like Math.Round except that it always
rounds up to the next
but this
double d = Math.Ceiling(29/15);
will get me "1" where I'd expect it to return "2". Am I on the wrong
track? Could somebody be so kind an shed some light?
29/15 is 1 in integer math.
Essentially its performing Floor(29/15) first or another way is [29/15] or
int(29/15).
The compiler has no way of knowing that you want to divide floating point
numbers. I find that annoying cause it can creap in bugs if your not
careful.
You can do 29.0/15.0 for 29.0F/15.0F. The first is double precision I
believe while the second is single. Else it defaults to int and performs
integer arithmetic.
You are correct though, Ceiling rounds up.
matthias s wrote:
I believed that Math.Ceiling is like Math.Round except that it always rounds
up to the next
but this
double d = Math.Ceiling(29/15);
will get me "1" where I'd expect it to return "2". Am I on the wrong track?
Could somebody be so kind an shed some light?
Others have already explained how you can do a floating point
division instead of an integer division.
But note that you should never have to do a conversion
from integer to floating point.
If it is literal constants you can just write:
double d = 2;
If it is floating point variables your approach will work:
double d = Math.Ceiling(a/b);
If it is integer variables you do not need Math.Ceiling:
double d = (a + b - 1) / b;
Arne
>
If it is integer variables you do not need Math.Ceiling:
double d = (a + b - 1) / b;
um,. if the lhs is done using all integer arithmetic then
(a + b - 1)/b = a/b + 1 - 1/b
but 1/b = 0
so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.
i.e.
a/b by itself is integer division which is equivalent to floor(a/b) if a and
b are treated as floating point(or even not since floor(floor(x)) =
floor(x). Since floor(x) + 1 = ceil(x) you can easily move between the two.
Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.
So rather,
int d = a / b + 1;
Not that your wrong but just wanted to clear up that issue.
Jon Slaughter <Jo***********@ Hotmail.comwrot e:
If it is integer variables you do not need Math.Ceiling:
double d = (a + b - 1) / b;
um,. if the lhs is done using all integer arithmetic then
(a + b - 1)/b = a/b + 1 - 1/b
You're applying algebra to split the whole thing up, which doesn't take
rounding into account. The rounding is only applied once in Arne's
post, instead of three times with your dissection.
but 1/b = 0
so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.
Yes there is. Take a=8, b=4. The ceiling of a/b is 2, but if you use
(a+b)/b you end up with 3. Take (a+b-1)/b you get 11/4 which is rounded
down to 2, the correct answer.
Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.
So rather,
int d = a / b + 1;
Again, that's wrong - when b is exactly divisible by a, the answer is
incorrect.
Not that your wrong but just wanted to clear up that issue.
Arne was absolutely correct, and you are wrong to apply algebra and
then integer division to each part and assume it's the same answer as
applying integer division to the original expression.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon Slaughter wrote:
>If it is integer variables you do not need Math.Ceiling:
double d = (a + b - 1) / b;
um,. if the lhs is done using all integer arithmetic then
(a + b - 1)/b = a/b + 1 - 1/b
but 1/b = 0
so your left with a/b + 1 which is the ceiling of course(since a/b is floor)
but no reason to subtract 1.
i.e.
a/b by itself is integer division which is equivalent to floor(a/b) if a and
b are treated as floating point(or even not since floor(floor(x)) =
floor(x). Since floor(x) + 1 = ceil(x) you can easily move between the two.
Point is, is that its unecessary to subtract 1/b as it does absolutely
nothing in integer arithmetic.
So rather,
int d = a / b + 1;
Not that your wrong but just wanted to clear up that issue.
Try put a = b into the two formulas:
(a + b - 1) / b
a / b + 1
Do you get the same ?
No !
So ...
Arne This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom |
last post by:
Has anyone ever seen a IComparer for floats the returns magnitude.
i.e. instead of returning -1, it would return -5. To let you know HOW
different the two numbers are. obviously for int it is a - b. But for
float the results would have to be normalize some how to a 32bit
range. I understand there would be percision errors.
Thanks
Tom
|
by: nick |
last post by:
I'm having a really weird problem. Take a look at this screenshot.
http://users.aber.ac.uk/njb4/dotnetproblem1.jpg
Notice the 4 watches at the bottom, _totalRecords, MyDataGrid.PageSize
and TotalPages.Text.
Now, you'd think that if _totalRecords was 11, and MyDataGrid.PageSize
was 10... If you divide them, you get 1.1. If you Ceiling 1.1 them
you'd get two.
|
by: Test User |
last post by:
Hi all,
I have learnt that if I want to round 0.5 to an integer the result should be
1, This is also the case if I do it in SQL server 2000, but if I do it in
VB.NET the result will be 0.
Other rounding results: 1.5 = 2 in SQL and 2 in VB.NET which I believe is
corrrect
2.5 = 3 in SQL and 2 in VB.NET which I believe is wrong.
|
by: Agnes |
last post by:
my number is 100.1 , Now I only want to get 100 without any round up or
round down function,
Can anyone told me which function can do that ?
Thanks
|
by: Ryan Liu |
last post by:
Is there a good reason that Decimal does not have Ceiling() method, only
Math class has?
Thanks!
| |
by: David Coleman |
last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
|
by: deanoooo812 |
last post by:
I have an Access query (written in MS Access 2000 - thats all we've got - don't get me started on that topic...) for making pharmacy dispensing labels based on an extract from an automated dispensing machine extract.
In this query, I use an expression to calculate the total tablet requirement from a combination of the table value of Sum of DailyQty * DaysSupply.
DaysSupply is a parameter that is entered by the user when running the query....
|
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= |
last post by:
Hi,
Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am interested in what is going on behind
the scenes and if there is some aspect of decimals that keep them from being
used in this calculation.
Thanks!
Ethan
Ethan Strauss Ph.D.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |