@@ -1227,10 +1227,7 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
1227
1227
/// ```
1228
1228
/// use ordered_float::NotNan;
1229
1229
///
1230
- /// let mut v = [
1231
- /// NotNan::new(2.0).unwrap(),
1232
- /// NotNan::new(1.0).unwrap(),
1233
- /// ];
1230
+ /// let mut v = [NotNan::new(2.0).unwrap(), NotNan::new(1.0).unwrap()];
1234
1231
/// v.sort();
1235
1232
/// assert_eq!(v, [1.0, 2.0]);
1236
1233
/// ```
@@ -1270,7 +1267,6 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
1270
1267
/// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and
1271
1268
/// `NotNan<T>`, as long as this does not create a NaN value.
1272
1269
/// However, consider using [`bytemuck`] as a safe alternative if possible.
1273
- ///
1274
1270
#[ cfg_attr(
1275
1271
not( feature = "bytemuck" ) ,
1276
1272
doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
@@ -1384,9 +1380,10 @@ impl NotNan<f64> {
1384
1380
/// Note: For the reverse conversion (from `NotNan<f32>` to `NotNan<f64>`), you can use
1385
1381
/// `.into()`.
1386
1382
pub fn as_f32 ( self ) -> NotNan < f32 > {
1387
- // This is not destroying invariants, as it is a pure rounding operation. The only two special
1388
- // cases are where f32 would be overflowing, then the operation yields Infinity, or where
1389
- // the input is already NaN, in which case the invariant is already broken elsewhere.
1383
+ // This is not destroying invariants, as it is a pure rounding operation. The only two
1384
+ // special cases are where f32 would be overflowing, then the operation yields
1385
+ // Infinity, or where the input is already NaN, in which case the invariant is
1386
+ // already broken elsewhere.
1390
1387
NotNan ( self . 0 as f32 )
1391
1388
}
1392
1389
}
@@ -1472,13 +1469,13 @@ impl<T: FloatCore> PartialEq<T> for NotNan<T> {
1472
1469
1473
1470
/// Adds a float directly.
1474
1471
///
1475
- /// Panics if the provided value is NaN or the computation results in NaN
1472
+ /// This returns a `T` and not a `NotNan<T>` because if the added value is NaN, this will be NaN
1476
1473
impl < T : FloatCore > Add < T > for NotNan < T > {
1477
- type Output = Self ;
1474
+ type Output = T ;
1478
1475
1479
1476
#[ inline]
1480
- fn add ( self , other : T ) -> Self {
1481
- NotNan :: new ( self . 0 + other) . expect ( "Addition resulted in NaN" )
1477
+ fn add ( self , other : T ) -> Self :: Output {
1478
+ self . 0 + other
1482
1479
}
1483
1480
}
1484
1481
@@ -1500,25 +1497,27 @@ impl<'a, T: FloatCore + Sum + 'a> Sum<&'a NotNan<T>> for NotNan<T> {
1500
1497
1501
1498
/// Subtracts a float directly.
1502
1499
///
1503
- /// Panics if the provided value is NaN or the computation results in NaN
1500
+ /// This returns a `T` and not a `NotNan<T>` because if the substracted value is NaN, this will be
1501
+ /// NaN
1504
1502
impl < T : FloatCore > Sub < T > for NotNan < T > {
1505
- type Output = Self ;
1503
+ type Output = T ;
1506
1504
1507
1505
#[ inline]
1508
- fn sub ( self , other : T ) -> Self {
1509
- NotNan :: new ( self . 0 - other) . expect ( "Subtraction resulted in NaN" )
1506
+ fn sub ( self , other : T ) -> Self :: Output {
1507
+ self . 0 - other
1510
1508
}
1511
1509
}
1512
1510
1513
1511
/// Multiplies a float directly.
1514
1512
///
1515
- /// Panics if the provided value is NaN or the computation results in NaN
1513
+ /// This returns a `T` and not a `NotNan<T>` because if the multiplied value is NaN, this will be
1514
+ /// NaN
1516
1515
impl < T : FloatCore > Mul < T > for NotNan < T > {
1517
- type Output = Self ;
1516
+ type Output = T ;
1518
1517
1519
1518
#[ inline]
1520
- fn mul ( self , other : T ) -> Self {
1521
- NotNan :: new ( self . 0 * other) . expect ( "Multiplication resulted in NaN" )
1519
+ fn mul ( self , other : T ) -> Self :: Output {
1520
+ self . 0 * other
1522
1521
}
1523
1522
}
1524
1523
@@ -1537,25 +1536,26 @@ impl<'a, T: FloatCore + Product + 'a> Product<&'a NotNan<T>> for NotNan<T> {
1537
1536
1538
1537
/// Divides a float directly.
1539
1538
///
1540
- /// Panics if the provided value is NaN or the computation results in NaN
1539
+ /// This returns a `T` and not a `NotNan<T>` because if the divided-by value is NaN, this will be
1540
+ /// NaN
1541
1541
impl < T : FloatCore > Div < T > for NotNan < T > {
1542
- type Output = Self ;
1542
+ type Output = T ;
1543
1543
1544
1544
#[ inline]
1545
- fn div ( self , other : T ) -> Self {
1546
- NotNan :: new ( self . 0 / other) . expect ( "Division resulted in NaN" )
1545
+ fn div ( self , other : T ) -> Self :: Output {
1546
+ self . 0 / other
1547
1547
}
1548
1548
}
1549
1549
1550
1550
/// Calculates `%` with a float directly.
1551
1551
///
1552
- /// Panics if the provided value is NaN or the computation results in NaN
1552
+ /// This returns a `T` and not a `NotNan<T>` because if the RHS is NaN, this will be NaN
1553
1553
impl < T : FloatCore > Rem < T > for NotNan < T > {
1554
- type Output = Self ;
1554
+ type Output = T ;
1555
1555
1556
1556
#[ inline]
1557
- fn rem ( self , other : T ) -> Self {
1558
- NotNan :: new ( self . 0 % other) . expect ( "Rem resulted in NaN" )
1557
+ fn rem ( self , other : T ) -> Self :: Output {
1558
+ self . 0 % other
1559
1559
}
1560
1560
}
1561
1561
@@ -1566,12 +1566,13 @@ macro_rules! impl_not_nan_binop {
1566
1566
1567
1567
#[ inline]
1568
1568
fn $method( self , other: Self ) -> Self {
1569
- self . $method( other. 0 )
1569
+ NotNan :: new( self . 0. $method( other. 0 ) )
1570
+ . expect( "Operation on two NotNan resulted in NaN" )
1570
1571
}
1571
1572
}
1572
1573
1573
1574
impl <T : FloatCore > $imp<& T > for NotNan <T > {
1574
- type Output = NotNan < T > ;
1575
+ type Output = T ;
1575
1576
1576
1577
#[ inline]
1577
1578
fn $method( self , other: & T ) -> Self :: Output {
@@ -1584,7 +1585,7 @@ macro_rules! impl_not_nan_binop {
1584
1585
1585
1586
#[ inline]
1586
1587
fn $method( self , other: & Self ) -> Self :: Output {
1587
- self . $method( other. 0 )
1588
+ self . $method( * other)
1588
1589
}
1589
1590
}
1590
1591
@@ -1593,7 +1594,7 @@ macro_rules! impl_not_nan_binop {
1593
1594
1594
1595
#[ inline]
1595
1596
fn $method( self , other: Self ) -> Self :: Output {
1596
- ( * self ) . $method( other. 0 )
1597
+ ( * self ) . $method( * other)
1597
1598
}
1598
1599
}
1599
1600
@@ -1602,12 +1603,12 @@ macro_rules! impl_not_nan_binop {
1602
1603
1603
1604
#[ inline]
1604
1605
fn $method( self , other: NotNan <T >) -> Self :: Output {
1605
- ( * self ) . $method( other. 0 )
1606
+ ( * self ) . $method( other)
1606
1607
}
1607
1608
}
1608
1609
1609
1610
impl <T : FloatCore > $imp<T > for & NotNan <T > {
1610
- type Output = NotNan < T > ;
1611
+ type Output = T ;
1611
1612
1612
1613
#[ inline]
1613
1614
fn $method( self , other: T ) -> Self :: Output {
@@ -1616,39 +1617,25 @@ macro_rules! impl_not_nan_binop {
1616
1617
}
1617
1618
1618
1619
impl <T : FloatCore > $imp<& T > for & NotNan <T > {
1619
- type Output = NotNan < T > ;
1620
+ type Output = T ;
1620
1621
1621
1622
#[ inline]
1622
1623
fn $method( self , other: & T ) -> Self :: Output {
1623
1624
( * self ) . $method( * other)
1624
1625
}
1625
1626
}
1626
1627
1627
- impl <T : FloatCore + $assign_imp> $assign_imp<T > for NotNan <T > {
1628
- #[ inline]
1629
- fn $assign_method( & mut self , other: T ) {
1630
- * self = ( * self ) . $method( other) ;
1631
- }
1632
- }
1633
-
1634
- impl <T : FloatCore + $assign_imp> $assign_imp<& T > for NotNan <T > {
1635
- #[ inline]
1636
- fn $assign_method( & mut self , other: & T ) {
1637
- * self = ( * self ) . $method( * other) ;
1638
- }
1639
- }
1640
-
1641
1628
impl <T : FloatCore + $assign_imp> $assign_imp for NotNan <T > {
1642
1629
#[ inline]
1643
1630
fn $assign_method( & mut self , other: Self ) {
1644
- ( * self ) . $assign_method ( other. 0 ) ;
1631
+ * self = ( * self ) . $method ( other) ;
1645
1632
}
1646
1633
}
1647
1634
1648
1635
impl <T : FloatCore + $assign_imp> $assign_imp<& Self > for NotNan <T > {
1649
1636
#[ inline]
1650
1637
fn $assign_method( & mut self , other: & Self ) {
1651
- ( * self ) . $assign_method ( other. 0 ) ;
1638
+ * self = ( * self ) . $method ( * other) ;
1652
1639
}
1653
1640
}
1654
1641
} ;
0 commit comments