◐ Shell
clean mode source ↗

Promote Int128/UInt128 arithmetic overflow to Double by KirtiRamchandani · Pull Request #27602 · PowerShell/PowerShell

@KirtiRamchandani

Summary

Fixes [Int128] and [UInt128] arithmetic overflow silently wrapping instead of promoting to [double] like [Int64].

Problem

[Int128]::MaxValue + [Int128]::MaxValue  # wrapped instead of promoting
([Int128]::MaxValue + [Int128]::MaxValue).GetType().Name  # was Int128, should be Double

Fix

  • Recognize Int128/UInt128 as numeric types
  • Add Int128Ops/UInt128Ops with BigInteger overflow detection
  • Route binary numeric operations through the custom numeric pipeline
  • Add Pester tests in ArithmeticOperators.Tests.ps1

Fixes #26677

Route Int128 and UInt128 through the numeric operations pipeline with
dedicated overflow handling, matching Int64 behavior instead of relying
on native wrapping operators.

Fixes PowerShell#26677

Copilot AI review requested due to automatic review settings

June 15, 2026 07:13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds Int128/UInt128 support to PowerShell numeric type handling and binary operator binding, including overflow behavior (promotion to Double) and targeted tests.

Changes:

  • Introduces Int128Ops / UInt128Ops implementations for arithmetic and comparisons.
  • Updates numeric type classification/helpers and type lists to include Int128 / UInt128.
  • Adds Pester tests validating overflow promotion to Double (and non-overflow preservation for Int128).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
test/powershell/Language/Operators/ArithmeticOperators.Tests.ps1 Adds tests for overflow promotion behavior for 64/128-bit integer arithmetic.
src/System.Management.Automation/utils/ExtensionMethods.cs Extends numeric-type helpers to treat Int128/UInt128 as numeric/integer.
src/System.Management.Automation/engine/runtime/Operations/NumericOps.cs Adds operator implementations for Int128 and UInt128.
src/System.Management.Automation/engine/runtime/Binding/Binders.cs Routes binary numeric ops to the new 128-bit operator implementations.
src/System.Management.Automation/engine/LanguagePrimitives.cs Includes Int128/UInt128 in numeric/integer type arrays used in conversions.

Comment on lines +2447 to +2456

else if (target.LimitType.IsInt128Type() || arg.LimitType.IsInt128Type())
{
opImplType = typeof(Int128Ops);
argType = typeof(Int128);
}
else if (target.LimitType.IsUInt128Type() || arg.LimitType.IsUInt128Type())
{
opImplType = typeof(UInt128Ops);
argType = typeof(UInt128);
}

Comment on lines +399 to +408

internal static object Add(Int128 lhs, Int128 rhs)
{
System.Numerics.BigInteger biResult = (System.Numerics.BigInteger)lhs + (System.Numerics.BigInteger)rhs;
if (biResult >= Int128.MinValue && biResult <= Int128.MaxValue)
{
return (Int128)biResult;
}

return (double)biResult;
}

Comment on lines +397 to +400

internal static class Int128Ops
{
internal static object Add(Int128 lhs, Int128 rhs)
{
return (double)biResult;
}

internal static object Sub(Int128 lhs, Int128 rhs)
return (double)biResult;
}

internal static object Multiply(Int128 lhs, Int128 rhs)
return (double)biResult;
}

internal static object Divide(Int128 lhs, Int128 rhs)
return (double)lhs / (double)rhs;
}

internal static object Remainder(Int128 lhs, Int128 rhs)
return lhs % rhs;
}

internal static object CompareEq(Int128 lhs, Int128 rhs) { return (lhs == rhs) ? Boxed.True : Boxed.False; }
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe "Arithmetic overflow promotion" -Tags "CI","RequireAdminOnWindows" {