Torch.add floating point addition

cbd: <p>In below code, “torch.add” gives some higher floating point value. Output is <strong>tensor([98310.1016])</strong><br>
<strong>after roundoff tensor([98310.1016])</strong></p>
<p>I want exact <strong>“98310.1”</strong> value. I try to round off but its not working. Any idea welcome.</p>
<pre><code class="lang-auto">import torch
a=torch.FloatTensor([98310])
b=0.1
arr=torch.add(a,b)
print(arr)

n_digits = 2
rounded = (arr * 10**n_digits).round() / (10**n_digits)
print("after roundoff" ,rounded)</code></pre>

KFrank: <p>Hi Cbd!</p>
<aside class="quote no-group" data-username="cbd" data-post="1" data-topic="134492" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/13edae/48.png" class="avatar"> cbd:</div>
<blockquote>
<p>Output is <strong>tensor([98310.1016])</strong><br>
<strong>after roundoff tensor([98310.1016])</strong></p>
<p>I want exact <strong>“98310.1”</strong> value. I try to round off but its not working.</p>
</blockquote>
</aside>
<p>The core problem is that 98310.1 is not exactly representable by<br>
the floating numbers that pytorch (and your cpu and gpu) use under<br>
the hood for arithmetic.  (Just to be clear, nor is 0.1.)</p>
<p>Please consider carefully the following:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.0'
&gt;&gt;&gt; torch.set_printoptions (precision=20)
&gt;&gt;&gt; bs = torch.tensor ([0.1], dtype = torch.float)
&gt;&gt;&gt; bs
tensor([0.10000000149011611938])
&gt;&gt;&gt; 1 + bs
tensor([1.10000002384185791016])
&gt;&gt;&gt; (1 + bs) - 1
tensor([0.10000002384185791016])
&gt;&gt;&gt; 98310 + bs
tensor([98310.10156250000000000000])
&gt;&gt;&gt; (98310 + bs) - 98310
tensor([0.10156250000000000000])
&gt;&gt;&gt; bd = torch.tensor ([0.1], dtype = torch.double)
&gt;&gt;&gt; bd
tensor([0.10000000000000000555], dtype=torch.float64)
&gt;&gt;&gt; 1 + bd
tensor([1.10000000000000008882], dtype=torch.float64)
&gt;&gt;&gt; (1 + bd) - 1
tensor([0.10000000000000008882], dtype=torch.float64)
&gt;&gt;&gt; 98310 + bd
tensor([98310.10000000000582076609], dtype=torch.float64)
&gt;&gt;&gt; (98310 + bd) - 98310
tensor([0.10000000000582076609], dtype=torch.float64)
</code></pre>
<p>The Wikipedia article on <a href="https://en.wikipedia.org/wiki/Round-off_error" rel="noopener nofollow ugc">round-off error</a> gives a good introduction to<br>
what is going on.</p>
<p>This is all directly relevant to the related question you raised in this<br>
other thread of yours:</p>
<aside class="quote quote-modified" data-post="1" data-topic="134404">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/13edae/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/floating-point-addition/134404">Floating point addition</a> 
  </div>
  <blockquote>
    I found strange behaviour while adding two tensor. Here “mat” tensor is output of mathematical equation and its precision value is 0 after decimal point ( I checked by placing torch.set_printoptions(precision=10) before print). After that if i do addition for below code it gives me output “98310.1016” instead of “98310.1”. Its very simple but i don’t know from where “.1016” comes in answer instead of “.1”. 
##    mat tensor([[98310., 98310., 98310.],
        [98310., 98310., 98310.],
        [98…
  </blockquote>
</aside>

<p>Best.</p>
<p>K. Frank</p>

cbd: <p>OK. I understood. Thanks.</p>