English 中文(简体)
长期。 MaxValue从单向长期造成多管齐下
原标题:Long.MaxValue conversion from Single to Long causing Overflow exception

<代码>Long.MaxValue>的价值9,223,372,036,854,775,807。 达到单一精确值后,即为9.223372E18。 然而, cast向一条龙卷土重来:<条码>。 尽管独家企业的价值实际上低于长期可能的最高价值,但这种情况仍然存在。 观察窗口中的实验表明,事实上,价值是

8.070450532247929344e18

is the largest double precision value that will safely ctype to a Long. further

8.070450807125836288e18

is the largest double precision value that after conversion to single precision, will safely convert to a Long.

这似乎同非常奇怪的行为一样。

为什么?

问题回答

你们的问题主要是“在转换为浮动点和重新注入愤怒时,有些愤怒者没有四舍五入?” 而且,由于ISO 754格式固有的精准问题。 NET用于其浮动。

如果您,则该代码为。 (C#,但在VB中运行。) NET或其中任何一种。 互联网支持的语言:

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine((ulong)long.MaxValue);
        Console.WriteLine((ulong)(float)long.MaxValue);
    }
}

最后,

9223372036854775807
9223372036854775808

通知第二号是怎样的。 如何做到这一点?

您可以安全地储存在浮动点号中的最大可能分类为:2^24,float2^53,两倍(即其红外线的借方数目,加标线的1)。 这符合你能够达到的最高精确度。 不能准确代表更多的人,因此,你可以接近。 事实上,根据:Wikipedia article on the subject:

Integers between 2^n and 2^(n+1) round to a multiple of 2^(n-23)

<代码>long.MaxValue过于庞大(其编号为<2^63-1<>>),因此没有准确储存。 而是四舍五入到<代码>2^39/代码>的多个。 页: 1 而且,这太大,难以长期适应,因此,在检查的情况下,你会例外。

If you put in 9223372036854775807 in a tool like this one, you ll see the value actually being stored is 9223372036854775808. Just as expected.

如果这确实是一个问题,你可以使用<代码>decimal ,该编码是四级的精度编号,可以安全地安装在<2>^-1<>>>>><113/code>以下的立体:

// This Just Works (tm)
Console.WriteLine((long)(decimal)long.MaxValue);

Or you could just not use floating point numbers.

Floating point numbers aren t broken, they re just built different.

当达到单一精确值时,<代码>Long.MaxValue即成为9.223372E18。

Although 9.223372E18 is the default string representation of the result, this is misleading. The exact result is not 9.223372 × 1018 but rather 263 = 9,223,372,036,854,775,808, which is the closest possible Single to the original value of 263 − 1. (The difference of 1 is due to the inability of Single to precisely represent the original value.)

由于2<63超过Long.MaxValue,试图将其重新编为>。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

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. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签