View source code
Display the source code in std/checkedint.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Function std.checkedint.Saturate.onOverflow
Implements saturation for operators +
, -
(unary and binary), *
, /
,
%
, ^^
, &
, |
, ^
, <<
, >>
, and >>>
.
auto onOverflow(string x, Lhs)
(
Lhs
);
typeof(Lhs()+Rhs()) onOverflow(string x, Lhs, Rhs)
(
Lhs lhs,
Rhs rhs
);
For unary -
, onOverflow
is called if lhs == Lhs
and Lhs
is a
signed type. The function returns Lhs
.
For binary operators, the result is as follows:
Lhs
if the result overflows in the positive direction, on division by.max 0
, or on shifting right by a negative valueLhs
if the result overflows in the negative direction.min 0
iflhs
is being shifted left by a negative value, or shifted right by a large positive value
Parameters
Name | Description |
---|---|
x | The operator involved in the opAssign operation |
Lhs | The left-hand side type of the operator (Lhs is the first argument to
Checked ) |
Rhs | The right-hand side type in the operator |
Returns
The saturated result of the operator.
Example
writeln(checked!Saturate(int .max) + 1); // int.max
writeln(checked!Saturate(100)^^10); // int.max
writeln(checked!Saturate(-100)^^10); // int.max
writeln(checked!Saturate(100) / 0); // int.max
writeln(checked!Saturate(100) << -1); // 0
writeln(checked!Saturate(100) << 33); // int.max
writeln(checked!Saturate(100) >> -1); // int.max
writeln(checked!Saturate(100) >> 33); // 0
}
Authors
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.