/
NonZeroFiniteFloat.scala
772 lines (718 loc) · 34.4 KB
/
NonZeroFiniteFloat.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* Copyright 2001-2016 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic.anyvals
import scala.collection.immutable.NumericRange
import scala.language.implicitConversions
import scala.util.{Try, Success, Failure}
import org.scalactic.{Validation, Pass, Fail}
import org.scalactic.{Or, Good, Bad}
/**
* An <code>AnyVal</code> for finite non-zero <code>Float</code>s.
*
* <p>
* Note: a <code>NonZeroFiniteFloat</code> may not equal 0.0.
* </p>
*
* <p>
* Because <code>NonZeroFiniteFloat</code> is an <code>AnyVal</code> it
* will usually be as efficient as an <code>Float</code>, being
* boxed only when an <code>Float</code> would have been boxed.
* </p>
*
* <p>
* The <code>NonZeroFiniteFloat.apply</code> factory method is implemented
* in terms of a macro that checks literals for validity at
* compile time. Calling <code>NonZeroFiniteFloat.apply</code> with a
* literal <code>Float</code> value will either produce a valid
* <code>NonZeroFiniteFloat</code> instance at run time or an error at
* compile time. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> import anyvals._
* import anyvals._
*
* scala> NonZeroFiniteFloat(1.1F)
* res0: org.scalactic.anyvals.NonZeroFiniteFloat = NonZeroFiniteFloat(1.1)
*
* scala> NonZeroFiniteFloat(0.0F)
* <console>:14: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F).
* NonZeroFiniteFloat(1.1F)
* ^
* </pre>
*
* <p>
* <code>NonZeroFiniteFloat.apply</code> cannot be used if the value being
* passed is a variable (<em>i.e.</em>, not a literal), because
* the macro cannot determine the validity of variables at
* compile time (just literals). If you try to pass a variable
* to <code>NonZeroFiniteFloat.apply</code>, you'll get a compiler error
* that suggests you use a different factor method,
* <code>NonZeroFiniteFloat.from</code>, instead:
* </p>
*
* <pre class="stREPL">
* scala> val x = 1.1F
* x: Float = 1.1
*
* scala> NonZeroFiniteFloat(x)
* <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a floating point literal, like NonZeroFiniteFloat(1.1F). Please use NonZeroFiniteFloat.from instead.
* NonZeroFiniteFloat(x)
* ^
* </pre>
*
* <p>
* The <code>NonZeroFiniteFloat.from</code> factory method will inspect
* the value at runtime and return an
* <code>Option[NonZeroFiniteFloat]</code>. If the value is valid,
* <code>NonZeroFiniteFloat.from</code> will return a
* <code>Some[NonZeroFiniteFloat]</code>, else it will return a
* <code>None</code>. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> NonZeroFiniteFloat.from(x)
* res3: Option[org.scalactic.anyvals.NonZeroFiniteFloat] = Some(NonZeroFiniteFloat(1.1))
*
* scala> val y = 0.0F
* y: Float = 0.0
*
* scala> NonZeroFiniteFloat.from(y)
* res4: Option[org.scalactic.anyvals.NonZeroFiniteFloat] = None
* </pre>
*
* <p>
* The <code>NonZeroFiniteFloat.apply</code> factory method is marked
* implicit, so that you can pass literal <code>Float</code>s
* into methods that require <code>NonZeroFiniteFloat</code>, and get the
* same compile-time checking you get when calling
* <code>NonZeroFiniteFloat.apply</code> explicitly. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> def invert(pos: NonZeroFiniteFloat): Float = Float.MaxValue - pos
* invert: (pos: org.scalactic.anyvals.NonZeroFiniteFloat)Float
*
* scala> invert(1.1F)
* res5: Float = 3.4028235E38
*
* scala> invert(Float.MaxValue)
* res6: Float = 0.0
*
* scala> invert(0.0F)
* <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F).
* invert(0.0F)
* ^
*
* scala> invert(0.0F)
* <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F).
* invert(0.0F)
* ^
*
* </pre>
*
* <p>
* This example also demonstrates that the <code>NonZeroFiniteFloat</code>
* companion object also defines implicit widening conversions
* when no loss of precision will occur. This makes it convenient to use a
* <code>NonZeroFiniteFloat</code> where a <code>Float</code> or wider
* type is needed. An example is the subtraction in the body of
* the <code>invert</code> method defined above,
* <code>Float.MaxValue - pos</code>. Although
* <code>Float.MaxValue</code> is a <code>Float</code>, which
* has no <code>-</code> method that takes a
* <code>NonZeroFiniteFloat</code> (the type of <code>pos</code>), you can
* still subtract <code>pos</code>, because the
* <code>NonZeroFiniteFloat</code> will be implicitly widened to
* <code>Float</code>.
* </p>
*
* @param value The <code>Float</code> value underlying this <code>NonZeroFiniteFloat</code>.
*/
final class NonZeroFiniteFloat private (val value: Float) extends AnyVal {
/**
* A string representation of this <code>NonZeroFiniteFloat</code>.
*/
override def toString: String = s"NonZeroFiniteFloat(${value.toString()}f)"
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Byte</code>.
*/
def toByte: Byte = value.toByte
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Short</code>.
*/
def toShort: Short = value.toShort
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Char</code>.
*/
def toChar: Char = value.toChar
/**
* Converts this <code>NonZeroFiniteFloat</code> to an <code>Int</code>.
*/
def toInt: Int = value.toInt
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Long</code>.
*/
def toLong: Long = value.toLong
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Float</code>.
*/
def toFloat: Float = value.toFloat
/**
* Converts this <code>NonZeroFiniteFloat</code> to a <code>Double</code>.
*/
def toDouble: Double = value.toDouble
/** Returns this value, unmodified. */
def unary_+ : NonZeroFiniteFloat = this
/** Returns the negation of this value. */
def unary_- : NonZeroFiniteFloat = NonZeroFiniteFloat.ensuringValid(-value)
/**
* Converts this <code>NonZeroFiniteFloat</code>'s value to a string then concatenates the given string.
*/
def +(x: String): String = s"${value.toString()}${x.toString()}"
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Byte): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Short): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Char): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Int): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Long): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Float): Boolean = value < x
/** Returns `true` if this value is less than x, `false` otherwise. */
def <(x: Double): Boolean = value < x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Byte): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Short): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Char): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Int): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Long): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Float): Boolean = value <= x
/** Returns `true` if this value is less than or equal to x, `false` otherwise. */
def <=(x: Double): Boolean = value <= x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Byte): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Short): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Char): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Int): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Long): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Float): Boolean = value > x
/** Returns `true` if this value is greater than x, `false` otherwise. */
def >(x: Double): Boolean = value > x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Byte): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Short): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Char): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Int): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Long): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Float): Boolean = value >= x
/** Returns `true` if this value is greater than or equal to x, `false` otherwise. */
def >=(x: Double): Boolean = value >= x
/** Returns the sum of this value and `x`. */
def +(x: Byte): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Short): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Char): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Int): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Long): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Float): Float = value + x
/** Returns the sum of this value and `x`. */
def +(x: Double): Double = value + x
/** Returns the difference of this value and `x`. */
def -(x: Byte): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Short): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Char): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Int): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Long): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Float): Float = value - x
/** Returns the difference of this value and `x`. */
def -(x: Double): Double = value - x
/** Returns the product of this value and `x`. */
def *(x: Byte): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Short): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Char): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Int): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Long): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Float): Float = value * x
/** Returns the product of this value and `x`. */
def *(x: Double): Double = value * x
/** Returns the quotient of this value and `x`. */
def /(x: Byte): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Short): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Char): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Int): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Long): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Float): Float = value / x
/** Returns the quotient of this value and `x`. */
def /(x: Double): Double = value / x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Byte): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Short): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Char): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Int): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Long): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Float): Float = value % x
/** Returns the remainder of the division of this value by `x`. */
def %(x: Double): Double = value % x
// Stuff from RichFloat
/**
* Returns <code>this</code> if <code>this > that</code> or <code>that</code> otherwise.
*/
def max(that: NonZeroFiniteFloat): NonZeroFiniteFloat = if (math.max(value, that.value) == value) this else that
/**
* Returns <code>this</code> if <code>this < that</code> or <code>that</code> otherwise.
*/
def min(that: NonZeroFiniteFloat): NonZeroFiniteFloat = if (math.min(value, that.value) == value) this else that
/**
* Indicates whether this `NonZeroFiniteFloat` has a value that is a whole number: it is finite and it has no fraction part.
*/
def isWhole = {
val longValue = value.toLong
longValue.toFloat == value || longValue == Long.MaxValue && value < Float.PositiveInfinity || longValue == Long.MinValue && value > Float.NegativeInfinity
}
/** Converts an angle measured in degrees to an approximately equivalent
* angle measured in radians.
*
* @return the measurement of the angle x in radians.
*/
def toRadians: Float = math.toRadians(value.toDouble).toFloat
/** Converts an angle measured in radians to an approximately equivalent
* angle measured in degrees.
* @return the measurement of the angle x in degrees.
*/
def toDegrees: Float = math.toDegrees(value.toDouble).toFloat
/**
* Applies the passed <code>Float => Float</code> function to the underlying <code>Float</code>
* value, and if the result is positive, returns the result wrapped in a <code>NonZeroFiniteFloat</code>,
* else throws <code>AssertionError</code>.
*
* <p>
* This method will inspect the result of applying the given function to this
* <code>NonZeroFiniteFloat</code>'s underlying <code>Float</code> value and if the result
* is finite non-zero, it will return a <code>NonZeroFiniteFloat</code> representing that value.
* Otherwise, the <code>Float</code> value returned by the given function is
* not finite non-zero, so this method will throw <code>AssertionError</code>.
* </p>
*
* <p>
* This method differs from a vanilla <code>assert</code> or <code>ensuring</code>
* call in that you get something you didn't already have if the assertion
* succeeds: a <em>type</em> that promises an <code>Float</code> is finite non-zero.
* With this method, you are asserting that you are convinced the result of
* the computation represented by applying the given function to this <code>NonZeroFiniteFloat</code>'s
* value will not produce invalid value.
* Instead of producing such invalid values, this method will throw <code>AssertionError</code>.
* </p>
*
* @param f the <code>Float => Float</code> function to apply to this <code>NonZeroFiniteFloat</code>'s
* underlying <code>Float</code> value.
* @return the result of applying this <code>NonZeroFiniteFloat</code>'s underlying <code>Float</code> value to
* to the passed function, wrapped in a <code>NonZeroFiniteFloat</code> if it is finite non-zero (else throws <code>AssertionError</code>).
* @throws AssertionError if the result of applying this <code>NonZeroFiniteFloat</code>'s underlying <code>Float</code> value to
* to the passed function is not finite non-zero.
*/
def ensuringValid(f: Float => Float): NonZeroFiniteFloat = {
val candidateResult: Float = f(value)
if (NonZeroFiniteFloatMacro.isValid(candidateResult)) new NonZeroFiniteFloat(candidateResult)
else throw new AssertionError(s"${candidateResult.toString()}, the result of applying the passed function to ${value.toString()}, was not a valid NonZeroFiniteFloat")
}
}
/**
* The companion object for <code>NonZeroFiniteFloat</code> that offers
* factory methods that produce <code>NonZeroFiniteFloat</code>s,
* implicit widening conversions from <code>NonZeroFiniteFloat</code> to
* other numeric types, and maximum and minimum constant values
* for <code>NonZeroFiniteFloat</code>.
*/
object NonZeroFiniteFloat {
/**
* The largest value representable as a finite non-zero <code>Float</code>,
* which is <code>NonZeroFiniteFloat(3.4028235E38)</code>.
*/
final val MaxValue: NonZeroFiniteFloat = NonZeroFiniteFloat.ensuringValid(Float.MaxValue)
/**
* The smallest value representable as a finite non-zero
* <code>Float</code>, which is <code>NonZeroFiniteFloat(-3.4028235E38)</code>.
*/
final val MinValue: NonZeroFiniteFloat = NonZeroFiniteFloat.ensuringValid(Float.MinValue) // Can't use the macro here
/**
* A factory method that produces an <code>Option[NonZeroFiniteFloat]</code> given a
* <code>Float</code> value.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>NonZeroFiniteFloat</code>
* representing that value wrapped in a <code>Some</code>. Otherwise, the passed <code>Float</code>
* value is not finite non-zero, so this method will return <code>None</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Float</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Float</code> values at run time.
* </p>
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return
* wrapped in a <code>Some[NonZeroFiniteFloat]</code>.
* @return the specified <code>Float</code> value wrapped in a
* <code>Some[NonZeroFiniteFloat]</code>, if it is finite non-zero, else
* <code>None</code>.
*/
def from(value: Float): Option[NonZeroFiniteFloat] =
if (NonZeroFiniteFloatMacro.isValid(value)) Some(new NonZeroFiniteFloat(value)) else None
/**
* A factory/assertion method that produces a <code>NonZeroFiniteFloat</code> given a
* valid <code>Float</code> value, or throws <code>AssertionError</code>,
* if given an invalid <code>Float</code> value.
*
* Note: you should use this method only when you are convinced that it will
* always succeed, i.e., never throw an exception. It is good practice to
* add a comment near the invocation of this method indicating ''why'' you think
* it will always succeed to document your reasoning. If you are not sure an
* `ensuringValid` call will always succeed, you should use one of the other
* factory or validation methods provided on this object instead: `isValid`,
* `tryingValid`, `passOrElse`, `goodOrElse`, or `rightOrElse`.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>NonZeroFiniteFloat</code> representing that value.
* Otherwise, the passed <code>Float</code> value is not finite non-zero, so
* this method will throw <code>AssertionError</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Float</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Float</code> values at run time.
* It differs from a vanilla <code>assert</code> or <code>ensuring</code>
* call in that you get something you didn't already have if the assertion
* succeeds: a <em>type</em> that promises a <code>Float</code> is positive.
* </p>
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return
* wrapped in a <code>NonZeroFiniteFloat</code>.
* @return the specified <code>Float</code> value wrapped in a
* <code>NonZeroFiniteFloat</code>, if it is finite non-zero, else
* throws <code>AssertionError</code>.
* @throws AssertionError if the passed value is not finite non-zero
*/
def ensuringValid(value: Float): NonZeroFiniteFloat =
if (NonZeroFiniteFloatMacro.isValid(value)) new NonZeroFiniteFloat(value) else {
throw new AssertionError(s"${value.toString()} was not a valid NonZeroFiniteFloat")
}
/**
* A factory/validation method that produces a <code>NonZeroFiniteFloat</code>, wrapped
* in a <code>Success</code>, given a valid <code>Float</code> value, or if the
* given <code>Float</code> is invalid, an <code>AssertionError</code>, wrapped
* in a <code>Failure</code>.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>NonZeroFiniteFloat</code>
* representing that value, wrapped in a <code>Success</code>.
* Otherwise, the passed <code>Float</code> value is not finite non-zero, so this
* method will return an <code>AssertionError</code>, wrapped in a <code>Failure</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Float</code> literals at compile time, whereas this method inspects
* <code>Float</code> values at run time.
* </p>
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return
* wrapped in a <code>Success(NonZeroFiniteFloat)</code>.
* @return the specified <code>Float</code> value wrapped
* in a <code>Success(NonZeroFiniteFloat)</code>, if it is finite non-zero, else a <code>Failure(AssertionError)</code>.
*/
def tryingValid(value: Float): Try[NonZeroFiniteFloat] =
if (NonZeroFiniteFloatMacro.isValid(value))
Success(new NonZeroFiniteFloat(value))
else
Failure(new AssertionError(s"${value.toString()} was not a valid NonZeroFiniteFloat"))
/**
* A validation method that produces a <code>Pass</code>
* given a valid <code>Float</code> value, or
* an error value of type <code>E</code> produced by passing the
* given <em>invalid</em> <code>Int</code> value
* to the given function <code>f</code>, wrapped in a <code>Fail</code>.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>Pass</code>.
* Otherwise, the passed <code>Float</code> value is finite non-zero, so this
* method will return a result of type <code>E</code> obtained by passing
* the invalid <code>Float</code> value to the given function <code>f</code>,
* wrapped in a `Fail`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Float</code> literals at compile time, whereas this method inspects
* <code>Float</code> values at run time.
* </p>
*
* @param value the `Float` to validate that it is finite non-zero.
* @return a `Pass` if the specified `Float` value is finite non-zero,
* else a `Fail` containing an error value produced by passing the
* specified `Float` to the given function `f`.
*/
def passOrElse[E](value: Float)(f: Float => E): Validation[E] =
if (NonZeroFiniteFloatMacro.isValid(value)) Pass else Fail(f(value))
/**
* A factory/validation method that produces a <code>NonZeroFiniteFloat</code>, wrapped
* in a <code>Good</code>, given a valid <code>Float</code> value, or if the
* given <code>Float</code> is invalid, an error value of type <code>B</code>
* produced by passing the given <em>invalid</em> <code>Float</code> value
* to the given function <code>f</code>, wrapped in a <code>Bad</code>.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>NonZeroFiniteFloat</code>
* representing that value, wrapped in a <code>Good</code>.
* Otherwise, the passed <code>Float</code> value is not finite non-zero, so this
* method will return a result of type <code>B</code> obtained by passing
* the invalid <code>Float</code> value to the given function <code>f</code>,
* wrapped in a `Bad`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Float</code> literals at compile time, whereas this method inspects
* <code>Float</code> values at run time.
* </p>
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return
* wrapped in a <code>Good(NonZeroFiniteFloat)</code>.
* @return the specified <code>Float</code> value wrapped
* in a <code>Good(NonZeroFiniteFloat)</code>, if it is finite non-zero, else a <code>Bad(f(value))</code>.
*/
def goodOrElse[B](value: Float)(f: Float => B): NonZeroFiniteFloat Or B =
if (NonZeroFiniteFloatMacro.isValid(value)) Good(NonZeroFiniteFloat.ensuringValid(value)) else Bad(f(value))
/**
* A factory/validation method that produces a <code>NonZeroFiniteFloat</code>, wrapped
* in a <code>Right</code>, given a valid <code>Int</code> value, or if the
* given <code>Int</code> is invalid, an error value of type <code>L</code>
* produced by passing the given <em>invalid</em> <code>Int</code> value
* to the given function <code>f</code>, wrapped in a <code>Left</code>.
*
* <p>
* This method will inspect the passed <code>Int</code> value and if
* it is a finite non-zero <code>Int</code>, it will return a <code>NonZeroFiniteFloat</code>
* representing that value, wrapped in a <code>Right</code>.
* Otherwise, the passed <code>Int</code> value is not finite non-zero, so this
* method will return a result of type <code>L</code> obtained by passing
* the invalid <code>Int</code> value to the given function <code>f</code>,
* wrapped in a `Left`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Int</code> literals at compile time, whereas this method inspects
* <code>Int</code> values at run time.
* </p>
*
* @param value the <code>Int</code> to inspect, and if finite non-zero, return
* wrapped in a <code>Right(NonZeroFiniteFloat)</code>.
* @return the specified <code>Int</code> value wrapped
* in a <code>Right(NonZeroFiniteFloat)</code>, if it is finite non-zero, else a <code>Left(f(value))</code>.
*/
def rightOrElse[L](value: Float)(f: Float => L): Either[L, NonZeroFiniteFloat] =
if (NonZeroFiniteFloatMacro.isValid(value)) Right(NonZeroFiniteFloat.ensuringValid(value)) else Left(f(value))
/**
* A predicate method that returns true if a given
* <code>Float</code> value is finite non-zero.
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return true.
* @return true if the specified <code>Float</code> is finite non-zero, else false.
*/
def isValid(value: Float): Boolean = NonZeroFiniteFloatMacro.isValid(value)
/**
* A factory method that produces a <code>NonZeroFiniteFloat</code> given a
* <code>Float</code> value and a default <code>NonZeroFiniteFloat</code>.
*
* <p>
* This method will inspect the passed <code>Float</code> value and if
* it is a finite non-zero <code>Float</code>, it will return a <code>NonZeroFiniteFloat</code> representing that value.
* Otherwise, the passed <code>Float</code> value is not finite non-zero, so this
* method will return the passed <code>default</code> value.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Float</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Float</code> values at run time.
* </p>
*
* @param value the <code>Float</code> to inspect, and if finite non-zero, return.
* @param default the <code>NonZeroFiniteFloat</code> to return if the passed
* <code>Float</code> value is not finite non-zero.
* @return the specified <code>Float</code> value wrapped in a
* <code>NonZeroFiniteFloat</code>, if it is finite non-zero, else the
* <code>default</code> <code>NonZeroFiniteFloat</code> value.
*/
def fromOrElse(value: Float, default: => NonZeroFiniteFloat): NonZeroFiniteFloat =
if (NonZeroFiniteFloatMacro.isValid(value)) new NonZeroFiniteFloat(value) else default
import language.experimental.macros
import scala.language.implicitConversions
/**
* A factory method, implemented via a macro, that produces a
* <code>NonZeroFiniteFloat</code> if passed a valid <code>Float</code>
* literal, otherwise a compile time error.
*
* <p>
* The macro that implements this method will inspect the
* specified <code>Float</code> expression at compile time. If
* the expression is a finite non-zero <code>Float</code> literal,
* it will return a <code>NonZeroFiniteFloat</code> representing that value.
* Otherwise, the passed <code>Float</code> expression is either a literal
* that is not finite non-zero, or is not a literal, so this method
* will give a compiler error.
* </p>
*
* <p>
* This factory method differs from the <code>from</code>
* factory method in that this method is implemented via a
* macro that inspects <code>Float</code> literals at compile
* time, whereas <code>from</code> inspects <code>Float</code>
* values at run time.
* </p>
*
* @param value the <code>Float</code> literal expression to
* inspect at compile time, and if finite non-zero, to return
* wrapped in a <code>NonZeroFiniteFloat</code> at run time.
* @return the specified, valid <code>Float</code> literal
* value wrapped in a <code>NonZeroFiniteFloat</code>. (If the
* specified expression is not a valid <code>Float</code>
* literal, the invocation of this method will not
* compile.)
*/
implicit def apply(value: Float): NonZeroFiniteFloat = macro NonZeroFiniteFloatMacro.apply
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to
* <code>Float</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the
* specified <code>NonZeroFiniteFloat</code>
*/
implicit def widenToFloat(pos: NonZeroFiniteFloat): Float = pos.value
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to
* <code>Double</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the
* specified <code>NonZeroFiniteFloat</code>, widened to
* <code>Double</code>.
*/
implicit def widenToDouble(pos: NonZeroFiniteFloat): Double = pos.value
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to <code>NonZeroFloat</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the specified <code>NonZeroFiniteFloat</code>,
* widened to <code>Float</code> and wrapped in a <code>NonZeroFloat</code>.
*/
implicit def widenToNonZeroFloat(pos: NonZeroFiniteFloat): NonZeroFloat = NonZeroFloat.ensuringValid(pos.value)
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to <code>NonZeroDouble</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the specified <code>NonZeroFiniteFloat</code>,
* widened to <code>Double</code> and wrapped in a <code>NonZeroDouble</code>.
*/
implicit def widenToNonZeroDouble(pos: NonZeroFiniteFloat): NonZeroDouble = NonZeroDouble.ensuringValid(pos.value)
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to <code>NonZeroFiniteDouble</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the specified <code>NonZeroFiniteFloat</code>,
* widened to <code>Double</code> and wrapped in a <code>NonZeroFiniteDouble</code>.
*/
implicit def widenToNonZeroFiniteDouble(pos: NonZeroFiniteFloat): NonZeroFiniteDouble = NonZeroFiniteDouble.ensuringValid(pos.value)
/**
* Implicit widening conversion from <code>NonZeroFiniteFloat</code> to <code>FiniteDouble</code>.
*
* @param pos the <code>NonZeroFiniteFloat</code> to widen
* @return the <code>Float</code> value underlying the specified <code>NonZeroFiniteFloat</code>,
* widened to <code>Double</code> and wrapped in a <code>FiniteDouble</code>.
*/
implicit def widenToFiniteDouble(pos: NonZeroFiniteFloat): FiniteDouble = FiniteDouble.ensuringValid(pos.value)
/**
* Implicit Ordering instance.
*/
implicit val ordering: Ordering[NonZeroFiniteFloat] =
new Ordering[NonZeroFiniteFloat] {
def compare(x: NonZeroFiniteFloat, y: NonZeroFiniteFloat): Int = x.toFloat.compare(y)
}
/**
* The smallest positive value greater than 0.0d representable as a <code>NonZeroFiniteFloat</code>, which is NonZeroFiniteFloat(1.4E-45).
*/
final val MinPositiveValue: NonZeroFiniteFloat = NonZeroFiniteFloat.ensuringValid(Float.MinPositiveValue)
}