Is it possible to force exponent or significand of a float to match another float (Python)?
<p>This is an interesting question that I was trying to work through the other day. Is it possible to force the significand or exponent of one <code>float</code> to be the same as another <code>float</code> in Python?</p>

<p>The question arises because I was trying to rescale some data so that the min and max match another data set. However, my rescaled data was slightly off (after about 6 decimal places) and it was enough to cause problems down the line.</p>

<p>To give an idea, I have <code>f1</code> and <code>f2</code> (<code>type(f1) == type(f2) == numpy.ndarray</code>). I want <code>np.max(f1) == np.max(f2) and np.min(f1) == np.min(f2)</code>. To achieve this, I do:</p>

<pre><code>import numpy as np

f2 = (f2-np.min(f2))/(np.max(f2)-np.min(f2)) # f2 is now between 0.0 and 1.0
f2 = f2*(np.max(f1)-np.min(f1)) + np.min(f1)  # f2 is now between min(f1) and max(f1)
</code></pre>

<p>The result (just as an example) would be:</p>

<pre><code>np.max(f1) # 5.0230593
np.max(f2) # 5.0230602 but I need 5.0230593 
</code></pre>

<p>My initial thought is that  forcing the exponent of the <code>float</code> would be the correct solution. I couldn't find much on it, so I made a <strong>workaround</strong> for my need:</p>

<pre><code>exp = 0
mm = np.max(f1)

# find where the decimal is
while int(10**exp*mm) == 0
  exp += 1

# add 4 digits of precision
exp += 4

scale = 10**exp

f2 = np.round(f2*scale)/scale
f1 = np.round(f1*scale)/scale
</code></pre>

<p>now <code>np.max(f2) == np.max(f1)</code></p>

<p>However, is there a better way? Did I do something wrong? Is it possible to reshape a <code>float</code> to be similar to another <code>float</code> (exponent or other means)? </p>

<p>EDIT: as was suggested, I am now using:</p>

<pre><code>scale = 10**(-np.floor(np.log10(np.max(f1))) + 4)
</code></pre>

<p>While my solution above will work (for my application), I'm interested to know if there's a solution that can somehow force the <code>float</code> to have the same exponent and/or significand so that the numbers will become identical. </p>