runtime: abort stack scan in cases that we cannot unwind the stack
[gcc.git] / libgo / go / math / dim.go
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package math
6
7 // Dim returns the maximum of x-y or 0.
8 //
9 // Special cases are:
10 // Dim(+Inf, +Inf) = NaN
11 // Dim(-Inf, -Inf) = NaN
12 // Dim(x, NaN) = Dim(NaN, x) = NaN
13 func Dim(x, y float64) float64 {
14 // The special cases result in NaN after the subtraction:
15 // +Inf - +Inf = NaN
16 // -Inf - -Inf = NaN
17 // NaN - y = NaN
18 // x - NaN = NaN
19 v := x - y
20 if v <= 0 {
21 // v is negative or 0
22 return 0
23 }
24 // v is positive or NaN
25 return v
26 }
27
28 // Max returns the larger of x or y.
29 //
30 // Special cases are:
31 // Max(x, +Inf) = Max(+Inf, x) = +Inf
32 // Max(x, NaN) = Max(NaN, x) = NaN
33 // Max(+0, ±0) = Max(±0, +0) = +0
34 // Max(-0, -0) = -0
35 func Max(x, y float64) float64 {
36 return max(x, y)
37 }
38
39 func max(x, y float64) float64 {
40 // special cases
41 switch {
42 case IsInf(x, 1) || IsInf(y, 1):
43 return Inf(1)
44 case IsNaN(x) || IsNaN(y):
45 return NaN()
46 case x == 0 && x == y:
47 if Signbit(x) {
48 return y
49 }
50 return x
51 }
52 if x > y {
53 return x
54 }
55 return y
56 }
57
58 // Min returns the smaller of x or y.
59 //
60 // Special cases are:
61 // Min(x, -Inf) = Min(-Inf, x) = -Inf
62 // Min(x, NaN) = Min(NaN, x) = NaN
63 // Min(-0, ±0) = Min(±0, -0) = -0
64 func Min(x, y float64) float64 {
65 return min(x, y)
66 }
67
68 func min(x, y float64) float64 {
69 // special cases
70 switch {
71 case IsInf(x, -1) || IsInf(y, -1):
72 return Inf(-1)
73 case IsNaN(x) || IsNaN(y):
74 return NaN()
75 case x == 0 && x == y:
76 if Signbit(x) {
77 return x
78 }
79 return y
80 }
81 if x < y {
82 return x
83 }
84 return y
85 }