Skip to content

Commit e0a46b6

Browse files
Merge pull request #174 from gabriel-samfira/use-decimal-for-tagged-floats
Use decimals instead of double for tagged floats
2 parents ab3975c + 9ccb746 commit e0a46b6

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

Tests/powershell-yaml.Tests.ps1

+52-27
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,27 @@ wishlist:
342342
- product : A Cool Book.
343343
quantity : 1
344344
description : I love that Cool Book.
345-
aStringTatLooksLikeAFloat: 55,34
346-
aStringThatLooksLikeAnInt: 2018+
347-
scientificNotationInt: 1e+3
348-
scientificNotationBigInt: 1e+40
349-
intWithTag: !!int "42"
350-
zeroIntWithTag: !!int "0"
351-
zeroIntWithoutTag: 0
352-
scientificNotationIntWithTag: !!int "1e+3"
353345
price : 55.34
354346
total: 4443.52
355347
int64: $([int64]::MaxValue)
356348
note: >
357349
I can't wait.
358350
To get that Cool Book.
359351
352+
intsAndDecimals:
353+
aStringTatLooksLikeAFloat: 55,34
354+
aStringThatLooksLikeAnInt: 2018+
355+
scientificNotationInt: 1e+3
356+
scientificNotationBigInt: 1e+40
357+
intWithTag: !!int "42"
358+
zeroIntWithTag: !!int "0"
359+
zeroIntWithoutTag: 0
360+
scientificNotationIntWithTag: !!int "1e+3"
361+
aDecimalWithATag: !!float "3.9999999999999990"
362+
aDecimalWithoutATag: 3.9999999999999990
363+
decimalInfinity: !!float ".inf"
364+
decimalNegativeInfinity: !!float "-.inf"
365+
360366
dates:
361367
- !!timestamp 2001-12-15T02:59:43.1Z
362368
- !!timestamp 2001-12-14t21:59:43.10-05:00
@@ -392,16 +398,22 @@ bools:
392398
quantity = 1;
393399
description = "I love that Cool Book.";
394400
price = 55.34;
395-
aStringTatLooksLikeAFloat = "55,34"
401+
}
402+
);
403+
intsAndDecimals = @{
404+
aStringTatLooksLikeAFloat = "55,34";
396405
aStringThatLooksLikeAnInt = "2018+"
397406
scientificNotationInt = [int32]1000
398407
scientificNotationBigInt = [System.Numerics.BigInteger]::Parse("10000000000000000000000000000000000000000")
399408
intWithTag = 42
400409
zeroIntWithTag = 0
401410
zeroIntWithoutTag = 0
402411
scientificNotationIntWithTag = 1000
403-
}
404-
);
412+
aDecimalWithATag = [decimal]::Parse("3.9999999999999990", [System.Globalization.CultureInfo]::InvariantCulture)
413+
aDecimalWithoutATag = [decimal]::Parse("3.9999999999999990", [System.Globalization.CultureInfo]::InvariantCulture)
414+
decimalInfinity = [double]::PositiveInfinity
415+
decimalNegativeInfinity = [double]::NegativeInfinity
416+
}
405417
total = 4443.52;
406418
int64 = ([int64]::MaxValue);
407419
note = ("I can't wait. To get that Cool Book.`n");
@@ -444,25 +456,38 @@ bools:
444456
$product['quantity'] | Should -Be $expectedProduct['quantity']
445457
$product['description'] | Should -Be $expectedProduct['description']
446458
$product['price'] | Should -Be $expectedProduct['price']
447-
$product['aStringTatLooksLikeAFloat'] | Should -Be $expectedProduct['aStringTatLooksLikeAFloat']
448-
$product['aStringTatLooksLikeAFloat'] | Should -BeOfType ([string])
449-
$product['aStringThatLooksLikeAnInt'] | Should -Be $expectedProduct['aStringThatLooksLikeAnInt']
450-
$product['aStringThatLooksLikeAnInt'] | Should -BeOfType ([string])
451-
$product['zeroIntWithTag'] | Should -Be $expectedProduct['zeroIntWithTag']
452-
$product['zeroIntWithTag'] | Should -BeOfType ([int32])
453-
$product['zeroIntWithoutTag'] | Should -Be $expectedProduct['zeroIntWithoutTag']
454-
$product['zeroIntWithoutTag'] | Should -BeOfType ([int32])
455-
$product['scientificNotationInt'] | Should -Be $expectedProduct['scientificNotationInt']
456-
$product['scientificNotationInt'] | Should -BeOfType ([int32])
457-
$product['scientificNotationBigInt'] | Should -Be $expectedProduct['scientificNotationBigInt']
458-
$product['scientificNotationBigInt'] | Should -BeOfType ([System.Numerics.BigInteger])
459-
$product['intWithTag'] | Should -Be $expectedProduct['intWithTag']
460-
$product['intWithTag'] | Should -BeOfType ([int32])
461-
$product['scientificNotationIntWithTag'] | Should -Be $expectedProduct['scientificNotationIntWithTag']
462-
$product['scientificNotationIntWithTag'] | Should -BeOfType ([int32])
459+
463460
$res['total'] | Should -Be $expected['total']
464461
$res['note'] | Should -Be $expected['note']
465462

463+
$expectedIntsAndDecimals = $expected['intsAndDecimals']
464+
465+
$intsAndDecimals = $res['intsAndDecimals']
466+
$intsAndDecimals['aStringTatLooksLikeAFloat'] | Should -Be $expectedIntsAndDecimals['aStringTatLooksLikeAFloat']
467+
$intsAndDecimals['aStringTatLooksLikeAFloat'] | Should -BeOfType ([string])
468+
$intsAndDecimals['aStringThatLooksLikeAnInt'] | Should -Be $expectedIntsAndDecimals['aStringThatLooksLikeAnInt']
469+
$intsAndDecimals['aStringThatLooksLikeAnInt'] | Should -BeOfType ([string])
470+
$intsAndDecimals['zeroIntWithTag'] | Should -Be $expectedIntsAndDecimals['zeroIntWithTag']
471+
$intsAndDecimals['zeroIntWithTag'] | Should -BeOfType ([int32])
472+
$intsAndDecimals['zeroIntWithoutTag'] | Should -Be $expectedIntsAndDecimals['zeroIntWithoutTag']
473+
$intsAndDecimals['zeroIntWithoutTag'] | Should -BeOfType ([int32])
474+
$intsAndDecimals['scientificNotationInt'] | Should -Be $expectedIntsAndDecimals['scientificNotationInt']
475+
$intsAndDecimals['scientificNotationInt'] | Should -BeOfType ([int32])
476+
$intsAndDecimals['scientificNotationBigInt'] | Should -Be $expectedIntsAndDecimals['scientificNotationBigInt']
477+
$intsAndDecimals['scientificNotationBigInt'] | Should -BeOfType ([System.Numerics.BigInteger])
478+
$intsAndDecimals['intWithTag'] | Should -Be $expectedIntsAndDecimals['intWithTag']
479+
$intsAndDecimals['intWithTag'] | Should -BeOfType ([int32])
480+
$intsAndDecimals['scientificNotationIntWithTag'] | Should -Be $expectedIntsAndDecimals['scientificNotationIntWithTag']
481+
$intsAndDecimals['scientificNotationIntWithTag'] | Should -BeOfType ([int32])
482+
$intsAndDecimals['aDecimalWithATag'] | Should -Be $expectedIntsAndDecimals['aDecimalWithATag']
483+
$intsAndDecimals['aDecimalWithATag'] | Should -BeOfType ([decimal])
484+
$intsAndDecimals['aDecimalWithoutATag'] | Should -Be $expectedIntsAndDecimals['aDecimalWithoutATag']
485+
$intsAndDecimals['aDecimalWithoutATag'] | Should -BeOfType ([decimal])
486+
$intsAndDecimals['decimalInfinity'] | Should -Be $expectedIntsAndDecimals['decimalInfinity']
487+
$intsAndDecimals['decimalInfinity'] | Should -BeOfType ([double])
488+
$intsAndDecimals['decimalNegativeInfinity'] | Should -Be $expectedIntsAndDecimals['decimalNegativeInfinity']
489+
$intsAndDecimals['decimalNegativeInfinity'] | Should -BeOfType ([double])
490+
466491
$res['dates'] | Should -Not -BeNullOrEmpty
467492
$res['dates'].Count | Should -Be $expected['dates'].Count
468493
for( $idx = 0; $idx -lt $expected['dates'].Count; ++$idx )

powershell-yaml.psm1

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function Convert-ValueToProperType {
168168
}
169169
"tag:yaml.org,2002:float" {
170170
$parsedValue = 0.0
171-
if ($infinityRegex.Matches($Node.Value)) {
171+
if ($infinityRegex.Matches($Node.Value).Count -gt 0) {
172172
$prefix = $Node.Value.Substring(0, 1)
173173
switch ($prefix) {
174174
"-" {
@@ -180,8 +180,8 @@ function Convert-ValueToProperType {
180180
}
181181
}
182182
}
183-
if (![double]::TryParse($Node.Value, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
184-
Throw ("failed to parse scalar {0} as double" -f $Node)
183+
if (![decimal]::TryParse($Node.Value, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
184+
Throw ("failed to parse scalar {0} as decimal" -f $Node)
185185
}
186186
return $parsedValue
187187
}

0 commit comments

Comments
 (0)