Math operations
Remarks
Numbers in scientific (exponential) notation
For example - Double.Parse():
$([System.Double]::Parse('1,19E+7'))
$([System.Double]::Parse('1.19E+7')) - Fail: The expression "[System.Double]::Parse(1.19E+7)" cannot be evaluated. Input string was not in a correct format.
To avoid error above (for your selected culture) you can use, for example:
$([System.Double]::Parse('1.19E+7', '$([System.Globalization.CultureInfo]::GetCultureInfo("en-US"))'))
$([System.Double]::Parse('1.19E+7', '$([System.Globalization.CultureInfo]::CurrentUICulture)'))
$([System.Double]::Parse('1.19E+7', '$([System.Globalization.CultureInfo]::CurrentUICulture.NumberFormat)'))
$([System.Double]::Parse('1.19E+7', $$([System.Globalization.CultureInfo]::CurrentUICulture)))
...
Difference between quotes. Exponential notation problem.
All data inside double quotes ".."
will be evaluated manually (standard moving: upward from deepest).
All data inside single quotes '..'
is not processed and will be entirely sent into engine for single final evaluation.
What it means, for example:
$([MSBuild]::Multiply("$([System.Math]::Log(2))", 16)) -> 1,10903548889591E+16
\ \_(1) 0,693147180559945_/
\_______________(2)__________________________________/
$([MSBuild]::Multiply('$([System.Math]::Log(2))', 16)) -> 11,0903548889591
\______________________(1)___________________________/
$([System.Math]::Exp(1.10903548889591E+16)) = ∞
$([System.Math]::Exp(11.0903548889591)) = 65535,9999999983
Other samples:
Expression | Evaluated value |
---|---|
$([System.Math]::Log(2)) | 0,693147180559945 |
$([MSBuild]::Multiply('$([System.Math]::Log(2))', 16)) | 11,0903548889591 |
$([System.Math]::Exp('$([MSBuild]::Multiply($([System.Math]::Log(2)), 16))')) | 65536 |
$([System.Math]::Exp($([MSBuild]::Multiply('$([System.Math]::Log(10))', 4))))
= 9999.99999999997
$([System.Math]::Exp($([MSBuild]::Multiply($([System.Math]::Log(10)), 4))))
= 10000.0000000002
$([System.Math]::Exp('$([MSBuild]::Multiply($([System.Math]::Log(10)), 4))'))
= 10000
#[$(
[System.Math]::Exp('$(
[MSBuild]::Multiply(
$([System.Math]::Log(10)),
4
))'
)
)]
= 10000
Popular methods
Information for this section is not complete or temporarily is not available. To fix it, click - Edit |
Add()
$(number = $([MSBuild]::Add(1024, 12)))
$(number = $([MSBuild]::Add($(number), 1)))
v0.12.8+ Contains syntactic sugar |
Subtract()
$(number = $([MSBuild]::Subtract(1024, 12)))
$(number = $([MSBuild]::Subtract($(number), 1)))
v0.12.8+ Contains syntactic sugar |
Min / Max
- 0 - n & n - 18:
$([System.Math]::Max(0, $(n)))
$([System.Math]::Min($(n), 18))
- n - m (
min(max($(n), $(val)), $(m))
):
$([System.Math]::Min( $([System.Math]::Max( $(n), $(val) )), $(m) ))
For errors: Fail: Value was either too large or too small for a ...
you should provide a correct type for specific methods:
int Min(int val1, int val2);
decimal Min(decimal val1, decimal val2);
double Min(double val1, double val2);
float Min(float val1, float val2);
ulong Min(ulong val1, ulong val2);
long Min(long val1, long val2);
...
i.e.: you can try like this:
$([System.Math]::Min('$([System.Math]::Max($([System.Int32]::Parse($(n))), $([System.Int32]::Parse($(val)))))', '$([System.Int32]::Parse($(m)))'))
etc.
Bit mask
Set
$(mask = 0)
$(mask = $([MSBuild]::BitwiseOr($(mask), 1)))
$(mask = $([MSBuild]::BitwiseOr($(mask), 4)))
$(mask = $([MSBuild]::BitwiseOr($(mask), 8)))
$(maskString = $([System.Convert]::ToString('$([System.Convert]::ToInt32($(mask)))', 2)))
Result: 1101
Check
#[$(v = 2)]
#[( $([MSBuild]::BitwiseAnd($(mask), $(v))) != 0 ){
"$(v)" is defined in the mask($(maskString))
}
else{
"$(v)" is not defined in the mask($(maskString))
}]
"2" is not defined in the mask(1101)
Samples
The numbers of modulo
- 0 - 99:
$([MSBuild]::Modulo($(num), 100))
0, 1, 2, 3, 4 ... 98, 99, 0, 1, 2 ...
- n - m (e.g. 10 - 99):
Same as above, only use limit like:
= (val % (max - min)) + min
#[$(
[MSBuild]::Add(
$(minrev),
$([MSBuild]::Modulo(
$(num),
$([MSBuild]::Subtract(
$(maxrev),
$(minrev)
))
))
)
)]
10, 11, 12, ... 98, 99, 10, 11, 12 ...
Raise number to the specified power
$([System.Math]::Pow(10, 4))
= 10000
or via exp:
$([System.Math]::Exp('$([MSBuild]::Multiply($([System.Math]::Log(10)), 4))'))
= 10000
References
MSBuild Property Functions - you can use any static method or property of these system classes:
- System.Math
- Other available arithmetic methods here
- System.Decimal
- System.Double
- System.UInt16
- System.UInt32
- System.UInt64
- System.Int16
- System.Int32
- System.Int64
- System.TimeSpan
- System.DateTime
- …