Add to Float arithmetic operators, is_finite,..., round, min/max.#1794
Add to Float arithmetic operators, is_finite,..., round, min/max.#1794alainfrisch merged 20 commits intoocaml:trunkfrom
Conversation
|
Since this PR adds a wealth of predicates could you maybe also add the |
|
The AppVeyor failure is due to old versions of Also, I think tests will be needed for the new functions. |
I saw... ☹ I thought that was something of the past... I'll try to do the additional work ASAP. |
a32d1d2 to
7b09918
Compare
|
@dbuenzli Wish granted! ☺ |
|
@mshinwell It seems Flambda transforms let z =
let x = -0. and y = +0. in
if mod_float x 1. >= 0. then
x
else if false then x else y
let () =
printf "%g\n" (1. /. z)prints |
|
Here is a minimal reproduction case: The problem is that flambda, when meeting the values of the I will open a PR fixing the issue shortly. |
|
#1810 fixes the issue mentioned in the previous comments. |
|
Rebased on trunk. |
| when [x] or [y] is [nan]. Moreover [max (-0.) (+0.) = +0.] *) | ||
|
|
||
| val minmax : float -> float -> float * float | ||
| (** [minmax x y] returns [(x, y)] is [x <= y] and [(y, x)] otherwise. |
There was a problem hiding this comment.
"(...) returns (...) is (...)" should be "(...) returns (...) if (...)".
| = "caml_nextafter_float" "caml_nextafter" [@@unboxed] [@@noalloc] | ||
| (** [nextafter x y] returns the next representable floating-point | ||
| value following [x] in the direction of [y]. If [y] is less than | ||
| [x], these functions will return the largest representable number |
There was a problem hiding this comment.
"these functions" seems to be from the C documentation...
There was a problem hiding this comment.
It is – the documentation is heavily inspired from the C one.
There was a problem hiding this comment.
I meant: shouldn't it be "this function"?
There was a problem hiding this comment.
I modified it in the commit below.
|
|
||
| external trunc : float -> float = "caml_trunc_float" "caml_trunc" | ||
| [@@unboxed] [@@noalloc] | ||
| (** [trunc x] round [x] to the nearest integer whose absolute value is |
There was a problem hiding this comment.
"round" should be "rounds" for consistency.
| (** [is_nan x] returns [true] if and only if [x] represents a [nan]. *) | ||
|
|
||
| val is_integer : float -> bool | ||
| (** Says whether the floating point is an integer. *) |
There was a problem hiding this comment.
"Says" should be "Say" for consistency.
| (** [is_infinite x] says whether [x] is [infinity] or [neg_infinity]. *) | ||
|
|
||
| val is_nan : float -> bool | ||
| (** [is_nan x] returns [true] if and only if [x] represents a [nan]. *) |
There was a problem hiding this comment.
("[nan]" instead of "a [nan]"?)
There was a problem hiding this comment.
I'm not sure what is the better (given that there are several NaNs, even though we do not distinguish them here).
There was a problem hiding this comment.
I am not sure, but I think the documentation tends
to use "nan" rather than "a nan" in other places.
There was a problem hiding this comment.
I can change it if you wish. Or maybe reformulate it as [x] is [nan].
There was a problem hiding this comment.
I do not have a strong opinion on this question;
maybe @Octachron or @gasche will.
There was a problem hiding this comment.
I like [x] is [nan]: it has the advantage of being correct when expanding it to [x] is not a number; moreover the Pervasives documentation already uses this wording.
| value following [x] in the direction of [y]. If [y] is less than | ||
| [x], these functions will return the largest representable number | ||
| less than [x]. If [x] equals [y], the function returns [y]. | ||
| If [x] or [y] is a [nan], a [nan] is returned. *) |
There was a problem hiding this comment.
("[nan]" instead of "a [nan]"?)
|
Rebased. |
|
@gasche Fine with me. |
|
I have to say I have mixed feelings about The introduction of |
|
To be clear, I would prefer to have the functions defined as: |
It's not different it gives you the next value greater than the current one in the domain of the data type. |
|
I do understand your point of view, but I suspect some |
|
And maybe some code would simply like to get a value that is greater than the one that is given as argument. As far as I'm concerned |
|
I would also be fine with My point is that most of the existing code abstracting over I just wanted to share a concern, you and the author of the |
Are there examples? In the end, the question is what semantics we want to have for |
To be fair, most of the examples I have in mind are so
I won't say they are interesting as aliases, merely that they |
|
I really prefer if we keep |
|
Out of curiosity, is |
|
|
|
@Chris00 thanks! |
|
Thanks @Chris00 for your efforts! |
Remark: The
roundfunction calls to C because the latter is usually implemented as ASM. Note that naiveroundfunctions do not work.