<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><font face="monospace">Floating point is tricky!</font></font><br>
<br>
<div class="moz-cite-prefix">On 1/26/2023 5:25 PM, <a class="moz-txt-link-abbreviated" href="mailto:forax@univ-mlv.fr">forax@univ-mlv.fr</a>
wrote:<br>
</div>
<blockquote type="cite" cite="mid:317612412.6193866.1674771952958.JavaMail.zimbra@u-pem.fr">
<div style="font-family: arial, helvetica, sans-serif; font-size:
12pt; color: #000000">
<div>Ahhh,<br>
</div>
<div>so i'm the one to blame, here.<br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>For whatever reason, i get the representational equivalence
wrong. <br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>Thanks for the explanation Joe, i stand corrected.<br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>Rémi<br data-mce-bogus="1">
</div>
<div><br>
</div>
<hr id="zwchr" data-marker="__DIVIDER__">
<div data-marker="__HEADERS__">
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From:
</b>"joe darcy" <a class="moz-txt-link-rfc2396E" href="mailto:joe.darcy@oracle.com"><joe.darcy@oracle.com></a><br>
<b>To: </b>"Remi Forax" <a class="moz-txt-link-rfc2396E" href="mailto:forax@univ-mlv.fr"><forax@univ-mlv.fr></a>, "Angelos
Bimpoudis" <a class="moz-txt-link-rfc2396E" href="mailto:angelos.bimpoudis@oracle.com"><angelos.bimpoudis@oracle.com></a><br>
<b>Cc: </b>"amber-dev" <a class="moz-txt-link-rfc2396E" href="mailto:amber-dev@openjdk.org"><amber-dev@openjdk.org></a><br>
<b>Sent: </b>Thursday, January 26, 2023 11:04:20 PM<br>
<b>Subject: </b>Re: Draft JEP on Primitive types in
patterns, instanceof, and switch<br>
</blockquote>
</div>
<div>
<style style="display:none;">P {margin-top:0;margin-bottom:0;}</style></div>
<div data-marker="__QUOTED_TEXT__">
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<p>Switching on floating-point values is perfectly
well-defined. Despite common presentation to the contrary,
floating-point numbers are not "fuzzy," they are a
precisely-defined subset of real numbers, with some
notable special cases.<br>
</p>
<p><br>
</p>
<p>As mentioned in the draft JEP, the matching for
floating-point values should use representational
equivalence, quoting from java.lang.Double:</p>
<p><br>
</p>
<p> </p>
<blockquote>The two floating-point values represent the same
IEEE 754 <i>datum</i>. In particular, for <a href="https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/Double.html#isFinite(double)" target="_blank" moz-do-not-send="true">finite</a>
values, the sign, <a href="https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/Math.html#getExponent(double)" target="_blank" moz-do-not-send="true">exponent</a>, and
significand components of the floating-point values are
the same. Under this relation:
<ul>
<li> <code>+0.0</code> and <code>-0.0</code> are
distinguished from each other. </li>
<li> every bit pattern encoding a NaN is considered
equivalent to each other </li>
<li> positive infinity is equivalent to positive
infinity; negative infinity is equivalent to negative
infinity. </li>
</ul>
Expressions implementing this equivalence relation
include:
<ul>
<li><code>Double.doubleToLongBits(a) ==
Double.doubleToLongBits(b)</code> </li>
<li><code>Double.valueOf(a).equals(Double.valueOf(b))</code>
</li>
<li><code>Double.compare(a, b) == 0</code> </li>
</ul>
Note that representation equivalence is often an
appropriate notion of equivalence to test the behavior of
<a href="https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/StrictMath.html" title="class in java.lang" target="_blank" moz-do-not-send="true">math libraries</a>. </blockquote>
<br>
<p>So, under this approach you could have <br>
</p>
<p><br>
</p>
<p>switch(d) {</p>
<p> case -0.0 -> ...<br>
case +0.0 -> ...<br>
case NaN -> ... // Handles all NaNs<br>
...<br>
</p>
<p>}<br>
</p>
<p><br>
</p>
<p>Operationally, this could be desugared to switching over
a long by first mapping the double to long via
doubleToLongBits (and *not* raw-long-bits).</p>
<p><br>
</p>
<p>HTH,</p>
<p><br>
</p>
<p>-Joe<br>
</p>
<p><br>
</p>
<div class="moz-cite-prefix">On 1/26/2023 5:15 AM, Remi
Forax wrote:<br>
</div>
<blockquote cite="mid:1882463428.5806520.1674738916735.JavaMail.zimbra@u-pem.fr">
<div style="font-family: arial, helvetica, sans-serif;
font-size: 12pt; color: #000000">
<div><br>
</div>
<div><br>
</div>
<hr id="zwchr">
<div>
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From:
</b>"Angelos Bimpoudis" <a class="moz-txt-link-rfc2396E" href="mailto:angelos.bimpoudis@oracle.com" target="_blank" moz-do-not-send="true"><angelos.bimpoudis@oracle.com></a><br>
<b>To: </b>"amber-dev" <a class="moz-txt-link-rfc2396E" href="mailto:amber-dev@openjdk.org" target="_blank" moz-do-not-send="true"><amber-dev@openjdk.org></a><br>
<b>Sent: </b>Thursday, January 26, 2023 10:48:47 AM<br>
<b>Subject: </b>Draft JEP on Primitive types in
patterns, instanceof, and switch<br>
</blockquote>
</div>
<div> </div>
<div>
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<div style="font-family: "Segoe UI",
"Segoe UI ", "Helvetica Neue",
sans-serif; font-size: 11pt; color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0"> <span style="color: rgb(0, 0, 0); font-family:
"Segoe UI", "Segoe UI ",
"Helvetica Neue", sans-serif;
font-size: 11pt;">Hello all,</span></div>
<div style="font-family: "Segoe UI",
"Segoe UI ", "Helvetica Neue",
sans-serif; font-size: 11pt; color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0"> <span style="color: rgb(0, 0, 0); font-family:
"Segoe UI", "Segoe UI ",
"Helvetica Neue", sans-serif;
font-size: 11pt;"><br>
</span></div>
<div style="font-family: "Segoe UI",
"Segoe UI ", "Helvetica Neue",
sans-serif; font-size: 11pt; color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0"> <span style="color: rgb(0, 0, 0); font-family:
"Segoe UI", "Segoe UI ",
"Helvetica Neue", sans-serif;
font-size: 11pt;">I would like to share this
draft JEP with you about primitive types in
patterns, instanceof, and switch:</span></div>
<div style="font-family: "Segoe UI",
"Segoe UI ", "Helvetica Neue",
sans-serif; font-size: 11pt; color: rgb(0, 0, 0);
background-color: rgb(255, 255, 255);" class="elementToProof ContentPasted0">
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0"><a class="moz-txt-link-freetext" href="https://openjdk.org/jeps/8288476" target="_blank" moz-do-not-send="true">https://openjdk.org/jeps/8288476</a><br data-mce-bogus="1">
</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0">"Enhance pattern
matching by allowing primitive types to appear
anywhere in patterns. Extend instanceof to
support primitive types, and extend switch to
allow primitive constants as case labels."</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0">Comments very much
welcomed!</div>
<div class="ContentPasted0"><br>
</div>
<div class="ContentPasted0">Many thanks,</div>
<div class="ContentPasted0">Angelos</div>
<br>
</div>
</blockquote>
<div><br>
</div>
<div>I still think that the semantics proposed for
pattern matching on primitive types is useless
complexity with the perverse side effect of
normalizing the usage of "default" in pattern
matching (too many examples of this JEP are using
"default") but we already discussed that.<br>
</div>
<div><br>
</div>
<div>Allowing switching on double and float constants
is just wrong.</div>
<div>Rust is actually trying to remove that feature<br>
</div>
<div> <a href="https://github.com/rust-lang/rust/issues/41255" class="moz-txt-link-freetext" target="_blank" moz-do-not-send="true">https://github.com/rust-lang/rust/issues/41255</a><br>
</div>
<div><br>
</div>
<div>I see no point to make the same mistake.<br>
</div>
<div><br>
</div>
<div>Otherwise, the rest is fine.<br>
</div>
<div><br>
</div>
<div>Rémi<br>
</div>
<div><br>
</div>
</div>
</div>
</blockquote>
<br>
</blockquote>
</div>
</div>
</blockquote>
<br>
</body>
</html>