<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dana Reyes</title>
    <description>The latest articles on DEV Community by Dana Reyes (@danareyes96631).</description>
    <link>https://dev.to/danareyes96631</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4058142%2F982e3529-7a21-485a-91b4-c0f13748236c.png</url>
      <title>DEV Community: Dana Reyes</title>
      <link>https://dev.to/danareyes96631</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danareyes96631"/>
    <language>en</language>
    <item>
      <title>Unit conversion bugs I hit building a dosing calculator</title>
      <dc:creator>Dana Reyes</dc:creator>
      <pubDate>Sat, 01 Aug 2026 15:08:38 +0000</pubDate>
      <link>https://dev.to/danareyes96631/unit-conversion-bugs-i-hit-building-a-dosing-calculator-2mcp</link>
      <guid>https://dev.to/danareyes96631/unit-conversion-bugs-i-hit-building-a-dosing-calculator-2mcp</guid>
      <description>&lt;p&gt;Most of the hard parts of a dosing calculator are not the arithmetic. They are the&lt;br&gt;
units, and the fact that a user will happily type a number in whichever unit their supplies&lt;br&gt;
happen to be labelled with.&lt;/p&gt;

&lt;p&gt;Three things that cost me real debugging time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Never store a mixed unit.&lt;/strong&gt; I started with a &lt;code&gt;dose&lt;/code&gt; column holding whatever the user typed&lt;br&gt;
and a &lt;code&gt;unit&lt;/code&gt; column beside it. Every query then had to branch. The fix was boring and correct:&lt;br&gt;
normalise to one base unit (micrograms) at the edge, store an integer, and format back to the&lt;br&gt;
display unit only in the view layer. Integers also sidestep the float problem below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Floating point ruins reconstitution math.&lt;/strong&gt; Reconstitution is a ratio: total substance&lt;br&gt;
divided by solvent volume gives concentration, and dose divided by concentration gives the&lt;br&gt;
volume to draw. Chain three float divisions and you get numbers like &lt;code&gt;0.30000000000000004&lt;/code&gt;&lt;br&gt;
units, which looks broken even though it is off by 1e-17. I moved the whole chain to integer&lt;br&gt;
micrograms and integer microlitres and rounded once, at the end, to the precision the syringe&lt;br&gt;
markings actually allow. Rounding once at the boundary rather than at each step is the whole&lt;br&gt;
trick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Round to the instrument, not to the number.&lt;/strong&gt; A syringe marked in 0.02 unit increments&lt;br&gt;
cannot deliver 0.317. Displaying a value nobody can measure is worse than displaying a&lt;br&gt;
slightly wrong one, because the user has to invent the rounding themselves. So the formatter&lt;br&gt;
takes the increment as an argument and snaps to it.&lt;/p&gt;

&lt;p&gt;None of this is novel, but the general lesson generalised well for me: any app where the user&lt;br&gt;
supplies a physical quantity wants a single canonical unit, integer storage, one rounding step,&lt;br&gt;
and a formatter that knows what the real world can actually resolve.&lt;/p&gt;

&lt;p&gt;I wrote most of this up while building &lt;a href="https://glp1.app/" rel="noopener noreferrer"&gt;a peptide tracker and calculator&lt;/a&gt;&lt;br&gt;
as a side project. The &lt;a href="https://glp1.app/" rel="noopener noreferrer"&gt;reconstitution calculator&lt;/a&gt; is the part where the&lt;br&gt;
integer rewrite paid for itself immediately.&lt;/p&gt;

&lt;p&gt;If you are building anything that takes a measurement from a human, do the normalisation at&lt;br&gt;
the boundary. It is much harder to retrofit later.&lt;/p&gt;

</description>
      <category>webdev</category>
    </item>
  </channel>
</rss>
