-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathfloat.m
1401 lines (1224 loc) · 39.5 KB
/
float.m
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 1994-1998,2001-2008,2010, 2012 The University of Melbourne.
% Copyright (C) 2013-2016, 2018-2020 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%---------------------------------------------------------------------------%
%
% File: float.m.
% Main author: fjh.
% Stability: medium.
%
% Floating point support.
%
% Floats are double precision, except in .spf grades where they
% are single precision.
%
% Note that implementations which support IEEE floating point
% should ensure that in cases where the only valid answer is a "NaN"
% (the IEEE float representation for "not a number"), the det
% functions here will halt with a runtime error (or throw an exception)
% rather than returning a NaN. Quiet (non-signalling) NaNs have a
% semantics which is not valid in Mercury, since they don't obey the
% axiom "all [X] X = X".
%
% XXX Unfortunately the current Mercury implementation does not
% do that on all platforms, since neither ANSI C nor POSIX provide
% any portable way of ensuring that floating point operations
% whose result is not representable will raise a signal rather
% than returning a NaN. (Maybe C9X will help...?)
% The behaviour is correct on Linux and Digital Unix,
% but not on Solaris, for example.
%
% IEEE floating point also specifies that some functions should
% return different results for +0.0 and -0.0, but that +0.0 and -0.0
% should compare equal. This semantics is not valid in Mercury,
% since it doesn't obey the axiom `all [F, X, Y] X = Y => F(X) = F(Y)'.
% Again, the resolution is that in Mercury, functions which would
% return different results for +0.0 and -0.0 should instead halt
% execution with a run-time error (or throw an exception).
%
% XXX Here too the current Mercury implementation does not
% implement the intended semantics correctly on all platforms.
%
% XXX On machines such as x86 which support extra precision
% for intermediate results, the results may depend on the
% level of optimization, in particular inlining and evaluation
% of constant expressions.
% For example, the goal `1.0/9.0 = std_util.id(1.0)/9.0' may fail.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module float.
:- interface.
:- import_module pretty_printer.
%---------------------------------------------------------------------------%
%
% Arithmetic functions.
%
% Addition.
%
:- func (float::in) + (float::in) = (float::uo) is det.
% Subtraction.
%
:- func (float::in) - (float::in) = (float::uo) is det.
% Multiplication.
%
:- func (float::in) * (float::in) = (float::uo) is det.
% Division.
% Throws a `domain_error' exception if the right operand is zero.
% See the comments at the top of math.m to find out how to disable
% this check.
%
:- func (float::in) / (float::in) = (float::uo) is det.
% unchecked_quotient(X, Y) is the same as X / Y, but the behaviour
% is undefined if the right operand is zero.
%
:- func unchecked_quotient(float::in, float::in) = (float::uo) is det.
% Unary plus.
%
:- func + (float::in) = (float::uo) is det.
% Unary minus.
%
:- func - (float::in) = (float::uo) is det.
%---------------------------------------------------------------------------%
%
% Comparison predicates.
%
% Less than.
%
:- pred (float::in) < (float::in) is semidet.
% Less than or equal.
%
:- pred (float::in) =< (float::in) is semidet.
% Greater than or equal.
%
:- pred (float::in) >= (float::in) is semidet.
% Greater than.
%
:- pred (float::in) > (float::in) is semidet.
%---------------------------------------------------------------------------%
%
% Conversion from integer types.
%
% Convert an int into float.
%
% The behaviour when the int exceeds the range of what can be exactly
% represented by a float is undefined.
%
:- func float(int) = float.
% Convert a signed 8-bit integer into a float.
% Always succeeds as all signed 8-bit integers have an exact
% floating-point representation.
%
:- func from_int8(int8) = float.
% Convert a signed 16-bit integer into a float.
% Always succeeds as all signed 16-bit integers have an exact
% floating-point representation.
%
:- func from_int16(int16) = float.
% Convert a signed 32-bit integer into a float.
% The behaviour when the integer exceeds the range of what can be
% exactly represented by a float is undefined.
%
:- func cast_from_int32(int32) = float.
% Convert a signed 64-bit integer into a float.
% The behaviour when the integer exceeds the range of what can be
% exactly represented by a float is undefined.
%
:- func cast_from_int64(int64) = float.
% Convert an unsigned 8-bit integer into a float.
% Always succeeds as all unsigned 8-bit integers have an exact
% floating-point representation.
%
:- func from_uint8(uint8) = float.
% Convert an unsigned 16-bit integer into a float.
% Always succeeds as all unsigned 16-bit integers have an exact
% floating-point representation.
%
:- func from_uint16(uint16) = float.
% Convert an unsigned 32-bit integer into a float.
% The behaviour when the integer exceeds the range of what can be
% exactly represented by a float is undefined.
%
:- func cast_from_uint32(uint32) = float.
% Convert an unsigned 64-bit integer into a float.
% The behaviour when the integer exceeds the range of what can be
% exactly represented by a float is undefined.
%
:- func cast_from_uint64(uint64) = float.
%---------------------------------------------------------------------------%
%
% Conversion to int.
%
% ceiling_to_int(X) returns the smallest integer not less than X.
%
:- func ceiling_to_int(float) = int.
% floor_to_int(X) returns the largest integer not greater than X.
%
:- func floor_to_int(float) = int.
% round_to_int(X) returns the integer closest to X.
% If X has a fractional value of 0.5, it is rounded up.
%
:- func round_to_int(float) = int.
% truncate_to_int(X) returns the integer closest to X such that
% |truncate_to_int(X)| =< |X|.
%
:- func truncate_to_int(float) = int.
%---------------------------------------------------------------------------%
%
% Miscellaneous functions.
%
% Absolute value.
%
:- func abs(float) = float.
% Maximum.
%
:- func max(float, float) = float.
% Minimum.
%
:- func min(float, float) = float.
% pow(Base, Exponent) returns Base raised to the power Exponent.
% Fewer domain restrictions than math.pow: works for negative Base,
% and pow(B, 0) = 1.0 for all B, even B=0.0.
% Only pow(0, <negative>) throws a `domain_error' exception.
%
:- func pow(float, int) = float.
% Compute a non-negative integer hash value for a float.
%
:- func hash(float) = int.
:- pred hash(float::in, int::out) is det.
%---------------------------------------------------------------------------%
%
% Classification.
%
% True iff the argument is of infinite magnitude.
%
:- pred is_infinite(float::in) is semidet.
% Synonym for the above.
%
:- pred is_inf(float::in) is semidet.
% True iff the argument is not-a-number (NaN).
%
:- pred is_nan(float::in) is semidet.
% True iff the argument is of infinite magnitude or not-a-number (NaN).
%
:- pred is_nan_or_infinite(float::in) is semidet.
% Synonym for the above.
%
:- pred is_nan_or_inf(float::in) is semidet.
% True iff the argument is not of infinite magnitude and is not a
% not-a-number (NaN) value.
%
:- pred is_finite(float::in) is semidet.
% True iff the argument is of zero magnitude.
%
:- pred is_zero(float::in) is semidet.
%---------------------------------------------------------------------------%
%
% System constants.
%
% Maximum finite floating-point number.
%
% max = (1 - radix ** mantissa_digits) * radix ** max_exponent
%
:- func max = float.
% Minimum normalised positive floating-point number.
%
% min = radix ** (min_exponent - 1)
%
:- func min = float.
% Positive infinity.
%
:- func infinity = float.
% Smallest number x such that 1.0 + x \= 1.0.
% This represents the largest relative spacing of two consecutive floating
% point numbers.
%
% epsilon = radix ** (1 - mantissa_digits)
%
:- func epsilon = float.
% Radix of the floating-point representation.
% In the literature, this is sometimes referred to as `b'.
%
:- func radix = int.
% The number of base-radix digits in the mantissa.
% In the literature, this is sometimes referred to as `p' or `t'.
%
:- func mantissa_digits = int.
% Minimum negative integer such that:
% radix ** (min_exponent - 1)
% is a normalised floating-point number. In the literature,
% this is sometimes referred to as `e_min'.
%
:- func min_exponent = int.
% Maximum integer such that:
% radix ** (max_exponent - 1)
% is a normalised floating-point number.
% In the literature, this is sometimes referred to as `e_max'.
%
:- func max_exponent = int.
%---------------------------------------------------------------------------%
% Convert a float to a pretty_printer.doc for formatting.
%
:- func float_to_doc(float) = doc.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- interface.
% These functions are hidden for now. `int' is not guaranteed to be able to
% represent a double, so we return strings for now. Endianness and treatment
% of special values also needs to be considered.
% Convert a float to an IEEE single-precision floating point value, then
% return the integer representation of the bit layout of that value as a
% string.
%
:- func float32_bits_string(float::in) = (string::uo) is det.
% Convert a float to an IEEE double-precision floating point value, then
% return the integer representation of the bit layout of that value as a
% string.
%
:- func float64_bits_string(float::in) = (string::uo) is det.
:- implementation.
:- import_module exception.
:- import_module int.
:- import_module string.
%
% Header files of mathematical significance.
%
:- pragma foreign_decl("C", "
#include <float.h>
#include <math.h>
#ifdef MR_HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
").
%---------------------------------------------------------------------------%
% The other arithmetic and comparison operators are builtins,
% which the compiler expands inline. We don't need to define them here.
:- pragma inline('/'/2).
X / Y = Z :-
( if float_domain_checks, Y = 0.0 then
throw(domain_error("float.'/': division by zero"))
else
Z = unchecked_quotient(X, Y)
).
% This code is included here rather than just calling the version in
% math.m because we currently don't do transitive inter-module inlining,
% so code which uses `/'/2 but doesn't import math.m couldn't have the
% domain check optimized away.
%
:- pred float_domain_checks is semidet.
:- pragma inline(float_domain_checks/0).
:- pragma foreign_proc("C",
float_domain_checks,
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
#ifdef ML_OMIT_MATH_DOMAIN_CHECKS
SUCCESS_INDICATOR = MR_FALSE;
#else
SUCCESS_INDICATOR = MR_TRUE;
#endif
").
:- pragma foreign_proc("C#",
float_domain_checks,
[thread_safe, promise_pure],
"
#if ML_OMIT_MATH_DOMAIN_CHECKS
SUCCESS_INDICATOR = false;
#else
SUCCESS_INDICATOR = true;
#endif
").
:- pragma foreign_proc("Java",
float_domain_checks,
[thread_safe, promise_pure],
"
SUCCESS_INDICATOR = true;
").
:- pragma foreign_proc("Erlang",
float_domain_checks,
[thread_safe, promise_pure],
"
SUCCESS_INDICATOR = true
").
%---------------------------------------------------------------------------%
%
% Conversion from integer types.
%
% For Java, overflows are not detected, so this must be tested for
% explicitly. So every time there's a cast to int, the bounds are
% checked first.
:- pragma foreign_proc("C",
float(IntVal::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = IntVal;
").
:- pragma foreign_proc("C#",
float(IntVal::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) IntVal;
").
:- pragma foreign_proc("Java",
float(IntVal::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) IntVal;
").
:- pragma foreign_proc("Erlang",
float(IntVal::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(IntVal)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
from_int8(Int8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = Int8Val;
").
:- pragma foreign_proc("C#",
from_int8(Int8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int8Val;
").
:- pragma foreign_proc("Java",
from_int8(Int8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int8Val;
").
:- pragma foreign_proc("Erlang",
from_int8(Int8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(Int8Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
from_uint8(UInt8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = UInt8Val;
").
:- pragma foreign_proc("C#",
from_uint8(UInt8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) UInt8Val;
").
:- pragma foreign_proc("Java",
from_uint8(UInt8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) (UInt8Val & 0xff);
").
:- pragma foreign_proc("Erlang",
from_uint8(UInt8Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(UInt8Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
from_int16(Int16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = Int16Val;
").
:- pragma foreign_proc("C#",
from_int16(Int16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int16Val;
").
:- pragma foreign_proc("Java",
from_int16(Int16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int16Val;
").
:- pragma foreign_proc("Erlang",
from_int16(Int16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(Int16Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
from_uint16(UInt16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = UInt16Val;
").
:- pragma foreign_proc("C#",
from_uint16(UInt16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) UInt16Val;
").
:- pragma foreign_proc("Java",
from_uint16(UInt16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) (UInt16Val & 0xffff);
").
:- pragma foreign_proc("Erlang",
from_uint16(UInt16Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(UInt16Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
cast_from_int32(Int32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = Int32Val;
").
:- pragma foreign_proc("C#",
cast_from_int32(Int32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int32Val;
").
:- pragma foreign_proc("Java",
cast_from_int32(Int32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int32Val;
").
:- pragma foreign_proc("Erlang",
cast_from_int32(Int32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(Int32Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
cast_from_uint32(UInt32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = UInt32Val;
").
:- pragma foreign_proc("C#",
cast_from_uint32(UInt32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) UInt32Val;
").
:- pragma foreign_proc("Java",
cast_from_uint32(UInt32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) (UInt32Val & 0xffffffff);
").
:- pragma foreign_proc("Erlang",
cast_from_uint32(UInt32Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(UInt32Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
cast_from_int64(Int64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = Int64Val;
").
:- pragma foreign_proc("C#",
cast_from_int64(Int64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int64Val;
").
:- pragma foreign_proc("Java",
cast_from_int64(Int64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) Int64Val;
").
:- pragma foreign_proc("Erlang",
cast_from_int64(Int64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(Int64Val)
").
%---------------------------------------------------------------------------%
:- pragma foreign_proc("C",
cast_from_uint64(UInt64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
FloatVal = UInt64Val;
").
:- pragma foreign_proc("C#",
cast_from_uint64(UInt64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) UInt64Val;
").
:- pragma foreign_proc("Java",
cast_from_uint64(UInt64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = (double) UInt64Val;
").
:- pragma foreign_proc("Erlang",
cast_from_uint64(UInt64Val::in) = (FloatVal::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
FloatVal = float(UInt64Val)
").
%---------------------------------------------------------------------------%
%
% Conversion to ints.
%
:- pragma foreign_proc("C",
ceiling_to_int(X::in) = (Ceil::out),
[will_not_call_mercury, promise_pure, thread_safe,
does_not_affect_liveness],
"
Ceil = (MR_Integer) ML_FLOAT_CEIL(X);
").
:- pragma foreign_proc("C#",
ceiling_to_int(X::in) = (Ceil::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Ceil = System.Convert.ToInt32(System.Math.Ceiling(X));
").
:- pragma foreign_proc("Java",
ceiling_to_int(X::in) = (Ceil::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
if (X > (double) java.lang.Integer.MAX_VALUE ||
X <= (double) java.lang.Integer.MIN_VALUE - 1)
{
throw new java.lang.RuntimeException(
""Overflow converting floating point to int"");
} else {
Ceil = (int) java.lang.Math.ceil(X);
}
").
:- pragma foreign_proc("Erlang",
ceiling_to_int(X::in) = (Ceil::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
T = erlang:trunc(X),
case (X - T) > 0 of
true ->
Ceil = T + 1;
false ->
Ceil = T
end
").
:- pragma foreign_proc("C",
floor_to_int(X::in) = (Floor::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
Floor = (MR_Integer) ML_FLOAT_FLOOR(X);
").
:- pragma foreign_proc("C#",
floor_to_int(X::in) = (Floor::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Floor = System.Convert.ToInt32(System.Math.Floor(X));
").
:- pragma foreign_proc("Java",
floor_to_int(X :: in) = (Floor :: out),
[will_not_call_mercury, promise_pure, thread_safe],
"
if (X >= (double) java.lang.Integer.MAX_VALUE + 1 ||
X < (double) java.lang.Integer.MIN_VALUE)
{
throw new java.lang.RuntimeException(
""Overflow converting floating point to int"");
} else {
Floor = (int) java.lang.Math.floor(X);
}
").
:- pragma foreign_proc("Erlang",
floor_to_int(X::in) = (Floor::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
T = erlang:trunc(X),
case (X - T) < 0 of
true ->
Floor = T - 1;
false ->
Floor = T
end
").
:- pragma foreign_proc("C",
round_to_int(X::in) = (Round::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
Round = (MR_Integer) ML_FLOAT_FLOOR(X + (MR_Float)0.5);
").
:- pragma foreign_proc("C#",
round_to_int(X::in) = (Round::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Round = System.Convert.ToInt32(System.Math.Floor(X + 0.5));
").
:- pragma foreign_proc("Java",
round_to_int(X::in) = (Round::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
if (X >= (double) java.lang.Integer.MAX_VALUE + 0.5 ||
X < (double) java.lang.Integer.MIN_VALUE - 0.5)
{
throw new java.lang.RuntimeException(
""Overflow converting floating point to int"");
} else {
Round = (int) java.lang.Math.round(X);
}
").
:- pragma foreign_proc("Erlang",
round_to_int(X::in) = (Round::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
% XXX erlang:round returns the closest integer to x, rounding to even when
% x is halfway between two integers.
Round = erlang:round(X)
").
:- pragma foreign_proc("C",
truncate_to_int(X::in) = (Trunc::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
Trunc = (MR_Integer) X;
").
:- pragma foreign_proc("C#",
truncate_to_int(X::in) = (Trunc::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Trunc = System.Convert.ToInt32(X);
").
:- pragma foreign_proc("Java",
truncate_to_int(X::in) = (Trunc::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
if (X >= (double) java.lang.Integer.MAX_VALUE + 1 ||
X <= (double) java.lang.Integer.MIN_VALUE - 1)
{
throw new java.lang.RuntimeException(
""Overflow converting floating point to int"");
} else {
Trunc = (int) X;
}
").
:- pragma foreign_proc("Erlang",
truncate_to_int(X::in) = (Trunc::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
Trunc = erlang:trunc(X)
").
%---------------------------------------------------------------------------%
%
% Miscellaneous functions.
%
abs(Num) = Abs :-
( if Num =< 0.0 then
Abs = - Num
else
Abs = Num
).
max(X, Y) = Max :-
( if X >= Y then
Max = X
else
Max = Y
).
min(X, Y) = Min :-
( if X =< Y then
Min = X
else
Min = Y
).
pow(Base, Exp) = Ans :-
( if Exp >= 0 then
Ans = multiply_by_pow(1.0, Base, Exp)
else
( if float_domain_checks, Base = 0.0 then
throw(domain_error("float.pow: zero base"))
else
Ans = unchecked_quotient(1.0,
multiply_by_pow(1.0, Base, -Exp))
% See below re use of unchecked_quotient.
)
).
% Returns Scale0 * (Base ** Exp) (where X ** 0 == 1.0 for all X).
% Requires that Exp >= 0.
% Uses a simple "Russian peasants" algorithm. O(lg(Exp+1)).
%
:- func multiply_by_pow(float, float, int) = float.
multiply_by_pow(Scale0, Base, Exp) = Result :-
( if Exp = 0 then
Result = Scale0
else
( if odd(Exp) then
Scale1 = Scale0 * Base
else
Scale1 = Scale0
),
Result = multiply_by_pow(Scale1, Base * Base, Exp div 2)
).
% The reason for using unchecked_quotient in float.pow is so
% that float.pow(+/-0.5, -1111) gives +/-infinity rather than
% a domain error. (N.B. This relies on unchecked_quotient(1.0,
% +/-0.0) giving +/-infinity, whereas the documentation in
% float.m says that the results are undefined.)
% Using Result = float.multiply_by_pow(1.0, 1.0 / Base, -Exp)
% would give the right behaviour for underflow, but isn't
% generally as accurate.
% (Efficiency note: An optimization used by `power' in SGI's STL
% implementation is to test for Exp=0 and (for non-zero Exp) handle
% low zero bits in Exp before calling this loop: the loop for the low
% zero bits needs only square Base, it needn't update Acc until the
% end of that loop at which point Acc can be simply assigned from the
% then-current value of Base. This optimization would be especially
% valuable for expensive `*' operations; maybe provide a
% std_util.monoid_pow(func(T,T)=T MonoidOperator, T Identity, int
% Exp, T Base) = T Result function to complement the existing
% std_util.pow function.)
%---------------------------------------------------------------------------%
% In hashing a float in .NET or Java, we ensure that the value is
% non-negative, as this condition is not guaranteed by either API.
:- pragma foreign_proc("C",
hash(F::in) = (H::out),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
H = MR_hash_float(F);
").
:- pragma foreign_proc("C#",
hash(F::in) = (H::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
int Code = F.GetHashCode();
if (Code < 0) {
H = -Code ^ 1;
} else {
H = Code;
}
").
:- pragma foreign_proc("Java",
hash(F::in) = (H::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
int Code = (new java.lang.Double(F)).hashCode();
if (Code < 0) {
H = -Code ^ 1;
} else {
H = Code;
}
").
:- pragma foreign_proc("Erlang",
hash(F::in) = (H::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
H = erlang:phash2(F)
").
hash(F, H) :-
H = hash(F).
%---------------------------------------------------------------------------%
is_infinite(F) :-
is_inf(F).
is_nan_or_infinite(Float) :-
is_nan_or_inf(Float).
is_nan_or_inf(Float) :-
( is_nan(Float)
; is_inf(Float)
).
:- pragma foreign_proc("C",
is_nan(Flt::in),
[will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail,
does_not_affect_liveness],
"
SUCCESS_INDICATOR = MR_is_nan(Flt);
").
:- pragma foreign_proc("C#",
is_nan(Flt::in),