Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Optimize Double(/Decimal)-to-Integer conversion #1020

Open
tats-u opened this issue Jun 27, 2023 · 4 comments
Open

VB -> C#: Optimize Double(/Decimal)-to-Integer conversion #1020

tats-u opened this issue Jun 27, 2023 · 4 comments

Comments

@tats-u
Copy link

tats-u commented Jun 27, 2023

VB.Net input code

Imports System

Public Class A
    Public Shared Sub Main()
        Dim foo = 0.6
        Dim bar = CInt(Math.Truncate(foo))
        Dim baz = CInt(Math.Ceiling(foo))
        Console.WriteLine(bar)
        Console.WriteLine(baz)
    End Sub
End Class

Erroneous output

using System;

public partial class A
{
    public static void Main()
    {
        double foo = 0.6d;
        int bar = (int)Math.Round(Math.Truncate(foo));
        int baz = (int)Math.Round(Math.Ceiling(foo));
        Console.WriteLine(bar);
        Console.WriteLine(baz);
    }
}

Extra verbose Math.Round

Expected output

using System;

public partial class A
{
    public static void Main()
    {
        double foo = 0.6d;
        int bar = (int)foo;
        int baz = (int)Math.Ceiling(foo);
        Console.WriteLine(bar);
        Console.WriteLine(baz);
    }
}

Decimal is not optimized by the latest VB compiler but I think it can be.

CInt(Decimal)System.Convert.ToInt32 (similar as (int)Math.Round)

Details

  • Product in use: online
  • Version in use: e.g. 5.6.3 or a commit hash (if it's a 3rd party tool using this library, try one of the above)
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.
@GrahamTheCoder
Copy link
Member

Just to make sure I understand, I assume you mean that methods already returning integers (floor, ceiling, truncate, round) don't need an additional round to be added?

@tats-u
Copy link
Author

tats-u commented Jun 28, 2023

Those methods return values whose types are same as input.
VB does bankers' rounding but C# does truncation when converting floating points to integers.

@tats-u tats-u changed the title VB -> C#: Optimize Double/Decimal-to-Integer conversion VB -> C#: Optimize Double(/Decimal)-to-Integer conversion Jun 28, 2023
@tats-u
Copy link
Author

tats-u commented Jun 28, 2023

https://sharplab.io/#v2:DYLgbgRgNAJiDUAfAkgWwA4HsBOAXAzgAQDKAnvrgKaoCwAUPQAoCuEwAlgMaEDCwAhviIBBeoXGEWbLiQAW/bJRglWhALL92AOwAUASjESjAEXapCAM0yZCAXkIAGAHQA2Q0fGnzEBXd7ItXB0NXFknABVsZi1OfiodK0w9AzoPCS9CHwAvPx4AoJCwnkp2Di0AcwTrZPcPHkwtfExgSicAdWx2KgAZbUodH2wUtPF6xubWjq7KXq1+7OGJAFEtZWJWehXlPkF8IA==

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue | DebuggableAttribute.DebuggingModes.DisableOptimizations)]
[assembly: AssemblyVersion("0.0.0.0")]

public class A
{
    public static void Main()
    {
        double num = 0.6;
        checked
        {
            int value = (int)num;
            int value2 = (int)Math.Ceiling(num);
            Console.WriteLine(value);
            Console.WriteLine(value2);
        }
    }
}

https://learn.microsoft.com/en-us/dotnet/api/system.math.truncate?view=net-7.0

Starting with Visual Basic 15.8, the performance of Double-to-integer conversion is optimized if you pass the value returned by the Truncate method to the any of the integral conversion functions, or if the Double value returned by Truncate is automatically converted to an integer with Option Strict set to Off. This optimization allows code to run faster -- up to twice as fast for code that does a large number of conversions to integer types. The following example illustrates such an optimized conversion:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants