开发者

Any way to have double->float conversion regarded as "widening"?

开发者 https://www.devze.com 2023-02-06 06:27 出处:网络
Is there any way to tell vb.net (2010) to allow expressions of type Double to be passed to parameters expecting Float, without having to use an explicit CSng() calls, other than by turning off all com

Is there any way to tell vb.net (2010) to allow expressions of type Double to be passed to parameters expecting Float, without having to use an explicit CSng() calls, other than by turning off all complaints related to narrowing conversions? Given that a Double->Single conversion will never throw an exception, and given that precision loss does not seem to be a criterion for regarding a conversion as "Narrowing" (Long->Double is considered "Widening" despite the fact that it involves an inexact conversion from an "exact-value"开发者_Python百科 type to an "approximate-value" type; a Double->Single conversion merely goes from an "approximate-value" type to a "somewhat-more-approximate-value" type, so it is in some sense less lossy).

I know turning off Option Strict would eliminate squawks about Double->Single conversion, but that is incredibly massively overkill. Is there any way to stifle all complaints about Double->Single, without stifling other possibly-useful squawks?

Edit

If Microsoft isn't going to allow code to be written in the most logical fashion, would it be preferable to litter the code with CSng() every time I call graphics methods, or define NewRectF and NewPointF methods which accept doubles and then use the RectF/PointF overloads for the graphics methods, or would something else be better still?


I believe not, and (more importantly) if you can, I advise to not do it. In the best case, your tests will find the faulty data that members of your team might create by such a sorry compiler flag. In the worst case, your application's faulty data will go completely unnoticed.

Argument:

I'm afraid your logic is fallacious. While it's true that long accurately represents integers (in the range of longs), while double is an approximation of real numbers, it's not true that the conversion from long to double is inexact. (If unsure about this, check it out yourself with the program below.) No data will be lost when casting from long to double, so it's by definition non-narrowing. Such casts are thus not going to introduce errors on their own (although various errors may be introduced by treating double as real numbers).

This is certainly not true when casting from doubles to floats. If that wouldn't introduce data loss, we would always use floats, as they use less memory). It may be OK in some cases to cast a double to float, but the programmer should "vouch" for it. This he does by explicitly casting (using CSng).

Function DataLostWhenCasting(ByVal l As Long)
    Dim d As Double
    Dim e As Double

    d = l ' The cast (implicit)
    ' Check for lost data:
    e = d - l
    If e > 0 Then
        Return True
    End If

    Return False
End Function

Sub Main()
    Console.WriteLine(DataLostWhenCasting(Long.MinValue))
    Console.WriteLine(DataLostWhenCasting(Long.MaxValue))
End Sub


no, it's not possible

you have to convert it

or convert your code to use only double or only float(single)

0

精彩评论

暂无评论...
验证码 换一张
取 消