English 中文(简体)
如何转换数学。 造成伤害的后果?
原标题:how to convert Math.Ceiling result to int?
  • 时间:2012-01-12 09:49:24
  •  标签:
  • c#

Math.Ceiling returns double because double may store much bigger numbers. However if i m sure that int type is capable to store the result how should I convert? Is it safe to cast (int) Math.Ceiling(... ?

最佳回答

如果你确信你不会越过 in的能力,那就应该完全安全。

int myInt = (int)Math.Ceiling(...);

如果您不了解约束,你可使用<代码>long而不是int

问题回答

从C++的做法来看,我将采用以下做法。 即使在最高收益为99.999.8美元或100 000美元时,它也保证取得正确结果。

var result = (int)(Math.Ceiling(value) + 0.5);

如果你相信实施《守则》,那么以下《守则》也应当发挥作用。

var result = Convert.ToInt32(value);

如果说速度快,那么马上。 净投入和产出的最高限额相当缓慢。 最快的是线性表达。 2.4 秒

警告:只有正面的<代码> 价值和divisor数值。

www.un.org/Depts/DGACM/index_french.htm

我来到这里,C/C++显然也发现了这一点。 此前的发展者:

var ceilingResult = (value / divisor) + (value % divisor == 0 ? 0 : 1);

根据我自己的10M频率基准,Math。 最高限额为2.4秒。 在指定职能中要求这种表达方式,需要约380个ms,并将其作为直线表达方式。

<>B> 简单的算术只有

还考虑使用@mafu的建议

var ceilingResult = (value + divisor - 1) / divisor;

https://stackoverflow.com/a/2745086/887092>。 编号C++回答,以供参考和验证。 >。

<>C> DivRem

https://stackoverflow.com/a/148734/887092“https://stackoverflow.com/a/148734/887092。 我注意到,有人提醒我注意万国邮联的指示。 见https://learn.microsoft.com/en-us/dotnet/api/system.math.divrem?view=netframework-4.8 。 <代码>Math.DivRemshould<>em> > 后经正式决定加入《万国邮联指令》。

var quotient = Math.DivRem(value, divisor, out long remainder);
var ceilingResult = quotient + (remainder == 0 ? 0 : 1);

[我尚未测试这一点]。 See https://stackoverflow.com/a/924160/887092 。 (在使用负印数的情况下)

Further optimisations might be possible for this - maybe with casting. In this answer, https://stackoverflow.com/a/924160/887092, an if-conditional statement is used - they are about the same.


执行情况:

  • Modulus: Has two operations that are added, but also a conditional branch.
  • Arithmetic: Has some additional mathematical operations in a sequence.
  • DivRem: Builds on the Modulus approach. If C# does resolve Math.DivRem to a single CPU instruction, this might be faster. Further optimisations might also be possible.

我不敢肯定这两者如何在各种结构中发挥作用。 但是,现在你们有探索的选择。


如果你想读物的话。 地面 投入和产出更加容易:

var floorResult = (value / divisor);

页: 1

int x = (int)Math.Ceiling(0.9); // 1

如果你不能确定,如果你回来的人数是高的,那么你总是能够打字和检查。 MaxValue

int oInt = Convert.ToInt32(Math.Ceiling(value));

Math.Ceiling Returncode>double以来,您希望将其改为int,使用 页: 1

double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98,
                   0, 9.113e-16, 103.919, 17834.191, Double.MaxValue };
int result;

foreach (double value in values)
{
   try {
      result = Convert.ToInt32(value);
      Console.WriteLine("Converted the {0} value  {1}  to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Int32 type.", value);
   }   
}                                 
//    -1.79769313486232E+308 is outside the range of the Int32 type.
//    -13800000000 is outside the range of the Int16 type.
//    Converted the Double value  -1023.299  to the Int32 value -1023.
//    Converted the Double value  -12.98  to the Int32 value -13.
//    Converted the Double value  0  to the Int32 value 0.
//    Converted the Double value  9.113E-16  to the Int32 value 0.
//    Converted the Double value  103.919  to the Int32 value 104.
//    Converted the Double value  17834.191  to the Int32 value 17834.
//    1.79769313486232E+308 is outside the range of the Int32 type.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...