1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class Main {
18 
main(String[] args)19   public static void main(String[] args) {
20     remFloat();
21     remDouble();
22   }
23 
remFloat()24   private static void remFloat() {
25     expectApproxEquals(1.98F, $opt$Rem(1.98F, 2F));
26     expectApproxEquals(0F, $opt$Rem(2F, 0.5F));
27     expectApproxEquals(0.09999F, $opt$Rem(1.0F, 0.1F));
28     expectApproxEquals(1.9F, $opt$Rem(6.5F, 2.3F));
29     expectApproxEquals(0.48F, $opt$Rem(1.98F, 1.5F));
30     expectApproxEquals(0.9999F, $opt$Rem(0.9999F, 1.222F));
31     expectApproxEquals(0.9999F, $opt$Rem(0.9999F, 1.0001F));
32     expectApproxEquals(-1.98F, $opt$Rem(-1.98F, 2F));
33     expectApproxEquals(-0F, $opt$Rem(-2F, 0.5F));
34     expectApproxEquals(-0.09999F, $opt$Rem(-1.0F, 0.1F));
35     expectApproxEquals(-1.9F, $opt$Rem(-6.5F, 2.3F));
36     expectApproxEquals(-0.48F, $opt$Rem(-1.98F, 1.5F));
37     expectApproxEquals(-0.9999F, $opt$Rem(-0.9999F, 1.222F));
38     expectApproxEquals(-0.9999F, $opt$Rem(-0.9999F, 1.0001F));
39     expectApproxEquals(1.98F, $opt$Rem(1.98F, -2F));
40     expectApproxEquals(0F, $opt$Rem(2F, -0.5F));
41     expectApproxEquals(0.09999F, $opt$Rem(1.0F, -0.1F));
42     expectApproxEquals(1.9F, $opt$Rem(6.5F, -2.3F));
43     expectApproxEquals(0.48F, $opt$Rem(1.98F, -1.5F));
44     expectApproxEquals(0.9999F, $opt$Rem(0.9999F, -1.222F));
45     expectApproxEquals(0.9999F, $opt$Rem(0.9999F, -1.0001F));
46     expectApproxEquals(-1.98F, $opt$Rem(-1.98F, -2F));
47     expectApproxEquals(-0F, $opt$Rem(-2F, -0.5F));
48     expectApproxEquals(-0.09999F, $opt$Rem(-1.0F, -0.1F));
49     expectApproxEquals(-1.9F, $opt$Rem(-6.5F, -2.3F));
50     expectApproxEquals(-0.48F, $opt$Rem(-1.98F, -1.5F));
51     expectApproxEquals(-0.9999F, $opt$Rem(-0.9999F, -1.222F));
52     expectApproxEquals(-0.9999F, $opt$Rem(-0.9999F, -1.0001F));
53 
54     expectApproxEquals(1.68267e-18F, $opt$Rem(61615.2F, -2.48699e-17F));
55     expectApproxEquals(-8.63819e-09F, $opt$Rem(-1.73479e+14F, 3.11154e-08F));
56     expectApproxEquals(1.10911e-12F, $opt$Rem(338122F, 4.57572e-12F));
57 
58     expectApproxEquals(2F, $opt$RemConst(6F));
59     expectApproxEquals(2F, $opt$Rem(5.1F, 3.1F));
60     expectApproxEquals(2.1F, $opt$Rem(5.1F, 3F));
61     expectApproxEquals(-2F, $opt$Rem(-5.1F, 3.1F));
62     expectApproxEquals(-2.1F, $opt$Rem(-5.1F, -3F));
63     expectApproxEquals(2F, $opt$Rem(6F, 4F));
64     expectApproxEquals(2F, $opt$Rem(6F, -4F));
65     expectApproxEquals(0F, $opt$Rem(6F, 3F));
66     expectApproxEquals(0F, $opt$Rem(6F, -3F));
67     expectApproxEquals(0F, $opt$Rem(6F, 1F));
68     expectApproxEquals(0F, $opt$Rem(6F, -1F));
69     expectApproxEquals(-1F, $opt$Rem(-7F, 3F));
70     expectApproxEquals(-1F, $opt$Rem(-7F, -3F));
71     expectApproxEquals(0F, $opt$Rem(6F, 6F));
72     expectApproxEquals(0F, $opt$Rem(-6F, -6F));
73     expectApproxEquals(7F, $opt$Rem(7F, 9F));
74     expectApproxEquals(7F, $opt$Rem(7F, -9F));
75     expectApproxEquals(-7F, $opt$Rem(-7F, 9F));
76     expectApproxEquals(-7F, $opt$Rem(-7F, -9F));
77     expectApproxEquals(0F, $opt$Rem(Float.MAX_VALUE, 1F));
78     expectApproxEquals(0F, $opt$Rem(Float.MAX_VALUE, -1F));
79     expectApproxEquals(0F, $opt$Rem(Float.MIN_VALUE, 1F));
80     expectApproxEquals(0F, $opt$Rem(Float.MIN_VALUE, -1F));
81     expectApproxEquals(0F, $opt$Rem(0F, 7F));
82     expectApproxEquals(0F, $opt$Rem(0F, Float.MAX_VALUE));
83     expectApproxEquals(0F, $opt$Rem(0F, Float.MIN_VALUE));
84     expectApproxEquals(0F, $opt$Rem(0F, Float.POSITIVE_INFINITY));
85     expectApproxEquals(0F, $opt$Rem(0F, Float.NEGATIVE_INFINITY));
86     expectApproxEquals(4F, $opt$Rem(4F, Float.POSITIVE_INFINITY));
87     expectApproxEquals(4F, $opt$Rem(4F, Float.NEGATIVE_INFINITY));
88     expectApproxEquals(-4F, $opt$Rem(-4F, Float.POSITIVE_INFINITY));
89     expectApproxEquals(-4F, $opt$Rem(-4F, Float.NEGATIVE_INFINITY));
90     expectApproxEquals(0F, $opt$Rem(Float.MIN_NORMAL, Float.MIN_VALUE));
91     expectApproxEquals(0F, $opt$Rem(Float.MIN_NORMAL, Float.MIN_NORMAL));
92     expectApproxEquals(0F, $opt$Rem(Float.MIN_VALUE, Float.MIN_VALUE));
93     expectApproxEquals(0F, $opt$Rem(Float.MAX_VALUE, Float.MIN_VALUE));
94     expectApproxEquals(0F, $opt$Rem(Float.MAX_VALUE, Float.MAX_VALUE));
95     expectApproxEquals(0F, $opt$Rem(Float.MAX_VALUE, Float.MIN_NORMAL));
96     expectApproxEquals(Float.MIN_NORMAL, $opt$Rem(Float.MIN_NORMAL, Float.MAX_VALUE));
97     expectApproxEquals(Float.MIN_NORMAL, $opt$Rem(Float.MIN_NORMAL, Float.NEGATIVE_INFINITY));
98     expectApproxEquals(Float.MIN_NORMAL, $opt$Rem(Float.MIN_NORMAL, Float.POSITIVE_INFINITY));
99     expectApproxEquals(Float.MIN_VALUE, $opt$Rem(Float.MIN_VALUE, Float.MAX_VALUE));
100     expectApproxEquals(Float.MIN_VALUE, $opt$Rem(Float.MIN_VALUE, Float.MIN_NORMAL));
101     expectApproxEquals(Float.MIN_VALUE, $opt$Rem(Float.MIN_VALUE, Float.NEGATIVE_INFINITY));
102     expectApproxEquals(Float.MIN_VALUE, $opt$Rem(Float.MIN_VALUE, Float.POSITIVE_INFINITY));
103     expectApproxEquals(Float.MAX_VALUE, $opt$Rem(Float.MAX_VALUE, Float.NEGATIVE_INFINITY));
104     expectApproxEquals(Float.MAX_VALUE, $opt$Rem(Float.MAX_VALUE, Float.POSITIVE_INFINITY));
105 
106     expectNaN($opt$Rem(Float.NaN, 3F));
107     expectNaN($opt$Rem(3F, Float.NaN));
108     expectNaN($opt$Rem(3F, 0F));
109     expectNaN($opt$Rem(1F, 0F));
110     expectNaN($opt$Rem(-1F, 0F));
111     expectNaN($opt$Rem(Float.NEGATIVE_INFINITY, Float.MIN_VALUE));
112     expectNaN($opt$Rem(Float.NEGATIVE_INFINITY, Float.MAX_VALUE));
113     expectNaN($opt$Rem(Float.NEGATIVE_INFINITY, Float.MIN_NORMAL));
114     expectNaN($opt$Rem(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
115     expectNaN($opt$Rem(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY));
116     expectNaN($opt$Rem(Float.POSITIVE_INFINITY, Float.MIN_VALUE));
117     expectNaN($opt$Rem(Float.POSITIVE_INFINITY, Float.MAX_VALUE));
118     expectNaN($opt$Rem(Float.POSITIVE_INFINITY, Float.MIN_NORMAL));
119     expectNaN($opt$Rem(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY));
120     expectNaN($opt$Rem(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
121   }
122 
remDouble()123   private static void remDouble() {
124     expectApproxEquals(1.98D, $opt$Rem(1.98D, 2D));
125     expectApproxEquals(0D, $opt$Rem(2D, 0.5D));
126     expectApproxEquals(0.09999D, $opt$Rem(1.0D, 0.1D));
127     expectApproxEquals(1.9D, $opt$Rem(6.5D, 2.3D));
128     expectApproxEquals(0.48D, $opt$Rem(1.98D, 1.5D));
129     expectApproxEquals(0.9999D, $opt$Rem(0.9999D, 1.222D));
130     expectApproxEquals(0.9999D, $opt$Rem(0.9999D, 1.0001D));
131     expectApproxEquals(-1.98D, $opt$Rem(-1.98D, 2D));
132     expectApproxEquals(-0D, $opt$Rem(-2D, 0.5D));
133     expectApproxEquals(-0.09999D, $opt$Rem(-1.0D, 0.1D));
134     expectApproxEquals(-1.9D, $opt$Rem(-6.5D, 2.3D));
135     expectApproxEquals(-0.48D, $opt$Rem(-1.98D, 1.5D));
136     expectApproxEquals(-0.9999D, $opt$Rem(-0.9999D, 1.222D));
137     expectApproxEquals(-0.9999D, $opt$Rem(-0.9999D, 1.0001D));
138     expectApproxEquals(1.98D, $opt$Rem(1.98D, -2D));
139     expectApproxEquals(0D, $opt$Rem(2D, -0.5D));
140     expectApproxEquals(0.09999D, $opt$Rem(1.0D, -0.1D));
141     expectApproxEquals(1.9D, $opt$Rem(6.5D, -2.3D));
142     expectApproxEquals(0.48D, $opt$Rem(1.98D, -1.5D));
143     expectApproxEquals(0.9999D, $opt$Rem(0.9999D, -1.222D));
144     expectApproxEquals(0.9999D, $opt$Rem(0.9999D, -1.0001D));
145     expectApproxEquals(-1.98D, $opt$Rem(-1.98D, -2D));
146     expectApproxEquals(-0D, $opt$Rem(-2D, -0.5D));
147     expectApproxEquals(-0.09999D, $opt$Rem(-1.0D, -0.1D));
148     expectApproxEquals(-1.9D, $opt$Rem(-6.5D, -2.3D));
149     expectApproxEquals(-0.48D, $opt$Rem(-1.98D, -1.5D));
150     expectApproxEquals(-0.9999D, $opt$Rem(-0.9999D, -1.222D));
151     expectApproxEquals(-0.9999D, $opt$Rem(-0.9999D, -1.0001D));
152 
153     expectApproxEquals(2D, $opt$RemConst(6D));
154     expectApproxEquals(2D, $opt$Rem(5.1D, 3.1D));
155     expectApproxEquals(2.1D, $opt$Rem(5.1D, 3D));
156     expectApproxEquals(-2D, $opt$Rem(-5.1D, 3.1D));
157     expectApproxEquals(-2.1D, $opt$Rem(-5.1D, -3D));
158     expectApproxEquals(2D, $opt$Rem(6D, 4D));
159     expectApproxEquals(2D, $opt$Rem(6D, -4D));
160     expectApproxEquals(0D, $opt$Rem(6D, 3D));
161     expectApproxEquals(0D, $opt$Rem(6D, -3D));
162     expectApproxEquals(0D, $opt$Rem(6D, 1D));
163     expectApproxEquals(0D, $opt$Rem(6D, -1D));
164     expectApproxEquals(-1D, $opt$Rem(-7D, 3D));
165     expectApproxEquals(-1D, $opt$Rem(-7D, -3D));
166     expectApproxEquals(0D, $opt$Rem(6D, 6D));
167     expectApproxEquals(0D, $opt$Rem(-6D, -6D));
168     expectApproxEquals(7D, $opt$Rem(7D, 9D));
169     expectApproxEquals(7D, $opt$Rem(7D, -9D));
170     expectApproxEquals(-7D, $opt$Rem(-7D, 9D));
171     expectApproxEquals(-7D, $opt$Rem(-7D, -9D));
172     expectApproxEquals(0D, $opt$Rem(Double.MAX_VALUE, 1D));
173     expectApproxEquals(0D, $opt$Rem(Double.MAX_VALUE, -1D));
174     expectApproxEquals(0D, $opt$Rem(Double.MIN_VALUE, 1D));
175     expectApproxEquals(0D, $opt$Rem(Double.MIN_VALUE, -1D));
176     expectApproxEquals(0D, $opt$Rem(0D, 7D));
177     expectApproxEquals(0D, $opt$Rem(0D, Double.MAX_VALUE));
178     expectApproxEquals(0D, $opt$Rem(0D, Double.MIN_VALUE));
179     expectApproxEquals(0D, $opt$Rem(0D, Double.POSITIVE_INFINITY));
180     expectApproxEquals(0D, $opt$Rem(0D, Double.NEGATIVE_INFINITY));
181     expectApproxEquals(4D, $opt$Rem(4D, Double.POSITIVE_INFINITY));
182     expectApproxEquals(4D, $opt$Rem(4D, Double.NEGATIVE_INFINITY));
183     expectApproxEquals(-4D, $opt$Rem(-4D, Double.POSITIVE_INFINITY));
184     expectApproxEquals(-4D, $opt$Rem(-4D, Double.NEGATIVE_INFINITY));
185     expectApproxEquals(0D, $opt$Rem(Double.MIN_NORMAL, Double.MIN_VALUE));
186     expectApproxEquals(0D, $opt$Rem(Double.MIN_NORMAL, Double.MIN_NORMAL));
187     expectApproxEquals(0D, $opt$Rem(Double.MIN_VALUE, Double.MIN_VALUE));
188     expectApproxEquals(0D, $opt$Rem(Double.MAX_VALUE, Double.MIN_VALUE));
189     expectApproxEquals(0D, $opt$Rem(Double.MAX_VALUE, Double.MAX_VALUE));
190     expectApproxEquals(0D, $opt$Rem(Double.MAX_VALUE, Double.MIN_NORMAL));
191     expectApproxEquals(Double.MIN_NORMAL, $opt$Rem(Double.MIN_NORMAL, Double.MAX_VALUE));
192     expectApproxEquals(Double.MIN_NORMAL, $opt$Rem(Double.MIN_NORMAL, Double.NEGATIVE_INFINITY));
193     expectApproxEquals(Double.MIN_NORMAL, $opt$Rem(Double.MIN_NORMAL, Double.POSITIVE_INFINITY));
194     expectApproxEquals(Double.MIN_VALUE, $opt$Rem(Double.MIN_VALUE, Double.MAX_VALUE));
195     expectApproxEquals(Double.MIN_VALUE, $opt$Rem(Double.MIN_VALUE, Double.MIN_NORMAL));
196     expectApproxEquals(Double.MIN_VALUE, $opt$Rem(Double.MIN_VALUE, Double.NEGATIVE_INFINITY));
197     expectApproxEquals(Double.MIN_VALUE, $opt$Rem(Double.MIN_VALUE, Double.POSITIVE_INFINITY));
198     expectApproxEquals(Double.MAX_VALUE, $opt$Rem(Double.MAX_VALUE, Double.NEGATIVE_INFINITY));
199     expectApproxEquals(Double.MAX_VALUE, $opt$Rem(Double.MAX_VALUE, Double.POSITIVE_INFINITY));
200 
201     expectNaN($opt$Rem(Double.NaN, 3D));
202     expectNaN($opt$Rem(3D, Double.NaN));
203     expectNaN($opt$Rem(3D, 0D));
204     expectNaN($opt$Rem(1D, 0D));
205     expectNaN($opt$Rem(-1D, 0D));
206     expectNaN($opt$Rem(Double.NEGATIVE_INFINITY, Double.MIN_VALUE));
207     expectNaN($opt$Rem(Double.NEGATIVE_INFINITY, Double.MAX_VALUE));
208     expectNaN($opt$Rem(Double.NEGATIVE_INFINITY, Double.MIN_NORMAL));
209     expectNaN($opt$Rem(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
210     expectNaN($opt$Rem(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));
211     expectNaN($opt$Rem(Double.POSITIVE_INFINITY, Double.MIN_VALUE));
212     expectNaN($opt$Rem(Double.POSITIVE_INFINITY, Double.MAX_VALUE));
213     expectNaN($opt$Rem(Double.POSITIVE_INFINITY, Double.MIN_NORMAL));
214     expectNaN($opt$Rem(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
215     expectNaN($opt$Rem(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
216   }
217 
$opt$Rem(float a, float b)218   static float $opt$Rem(float a, float b) {
219     return a % b;
220   }
221 
$opt$RemConst(float a)222  static float $opt$RemConst(float a) {
223     return a % 4F;
224   }
225 
$opt$Rem(double a, double b)226   static double $opt$Rem(double a, double b) {
227     return a % b;
228   }
229 
$opt$RemConst(double a)230   static double $opt$RemConst(double a) {
231     return a % 4D;
232   }
233 
expectApproxEquals(float a, float b)234   public static void expectApproxEquals(float a, float b) {
235     float maxDelta = 0.00001F;
236     boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta);
237     if (!aproxEquals) {
238       throw new Error("Expected: " + a + ", found: " + b
239           + ", with delta: " + maxDelta + " " + (a - b));
240     }
241   }
242 
expectApproxEquals(double a, double b)243   public static void expectApproxEquals(double a, double b) {
244     double maxDelta = 0.00001D;
245     boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta);
246     if (!aproxEquals) {
247       throw new Error("Expected: " + a + ", found: "
248           + b + ", with delta: " + maxDelta + " " + (a - b));
249     }
250   }
251 
expectNaN(float a)252   public static void expectNaN(float a) {
253     if (a == a) {
254       throw new Error("Expected NaN: " + a);
255     }
256   }
257 
expectNaN(double a)258   public static void expectNaN(double a) {
259     if (a == a) {
260       throw new Error("Expected NaN: " + a);
261     }
262   }
263 
264 }
265