<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Engineering Dosage]]></title><description><![CDATA[A newsletter where I regularly share about software development, ML, open source, and much more.]]></description><link>https://srini047.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!enaL!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F068e8d2e-3e5c-4beb-afbc-3407be12e494_256x256.png</url><title>Engineering Dosage</title><link>https://srini047.substack.com</link></image><generator>Substack</generator><lastBuildDate>Thu, 30 Apr 2026 08:45:33 GMT</lastBuildDate><atom:link href="https://srini047.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Sriniketh]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[srini047@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[srini047@substack.com]]></itunes:email><itunes:name><![CDATA[Sriniketh]]></itunes:name></itunes:owner><itunes:author><![CDATA[Sriniketh]]></itunes:author><googleplay:owner><![CDATA[srini047@substack.com]]></googleplay:owner><googleplay:email><![CDATA[srini047@substack.com]]></googleplay:email><googleplay:author><![CDATA[Sriniketh]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[GCC Compiler Optimization Flags]]></title><description><![CDATA[-O1, O2, O3]]></description><link>https://srini047.substack.com/p/gcc-compiler-optimization-flags</link><guid isPermaLink="false">https://srini047.substack.com/p/gcc-compiler-optimization-flags</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Fri, 19 Jul 2024 19:13:24 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!eAO0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eAO0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eAO0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eAO0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:23531,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eAO0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!eAO0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16bfcf94-d91e-4aba-9df4-9c4efd9e45b0_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The GNU Compiler Collection, commonly known as GCC has a set of compilers and development for most distros. It has support for primarily C and C++ and other languages as well. Our main focus is on the various optimization flags.</p><p>Note: We shall consider only `C++` for simplicity.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>Levels of Optimization</h2><p>There are majorly three levels of optimizations that are frequently used with increasing order of optimization level. They are:</p><ul><li><p>-O1</p></li><li><p>-O2</p></li><li><p>-O3</p></li></ul><p>By default, -O optimization is applied where the compiler tries to reduce code size and execution time and provide much execution time. Then we can see minor/major improvements based on the code and assembly generated. Besides these, GCC also provides many other optimization flags. The entire list is available <a href="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html">here</a>.</p><h2>Assembly?</h2><p>Yes, you heard it right the C++ code written gets compiled initially by an Assembler and generates an object file `.o`. We can see the magic of these optimization flags in the generated assembly. Let&#8217;s consider the problem of calculating the sum of the first <code>10001</code> natural numbers. We can say it using the formula (n*(n+1)/2). But let&#8217;s write a naive way of calculating it using a simple for loop and see how the compiler arrives at the direct answer by activating different optimization flags.</p><pre><code>int calc_sum()
{
    int ans = 0;
    for(int i = 0; i &lt; 10001; ++i)
        ans += i;

    return ans;
}</code></pre><p>When we compile it without any flag we can see a naive code translated to assembly. I'm glad to be from ECE to understand the assembly code partially &#129319;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aKvS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aKvS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 424w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 848w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 1272w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aKvS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic" width="932" height="782" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:782,&quot;width&quot;:932,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:46598,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aKvS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 424w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 848w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 1272w, https://substackcdn.com/image/fetch/$s_!aKvS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0018de94-ec55-47f3-90fe-bdba79ee0504_932x782.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Now let&#8217;s see the magic of the GCC compiler and how it translates the code by enabling the -O1 flag.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tp4t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tp4t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 424w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 848w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 1272w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tp4t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic" width="734" height="384" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:384,&quot;width&quot;:734,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15519,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tp4t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 424w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 848w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 1272w, https://substackcdn.com/image/fetch/$s_!tp4t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a0a9036-dc30-4471-8821-c74dde3b2237_734x384.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Boom, we can see that it directly loads the end value inside a register, then subtracts one for `(end - 1)` in the for loop. Before returning, a direct calculation `50005000` without any looping.</p><p>Amazing right but this is at least not the end, we can further improve the optimization by enabling the `-O2` and `-O3` flags. For this snippet, the assembly code is the same.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!q5sQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!q5sQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 424w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 848w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 1272w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!q5sQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic" width="678" height="174" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:174,&quot;width&quot;:678,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10047,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!q5sQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 424w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 848w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 1272w, https://substackcdn.com/image/fetch/$s_!q5sQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F155366d2-8233-4a14-8547-2b80b32300d7_678x174.heic 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p> &#127881; Direct computation of the result just by storing the exact required value in the register. All this is not magic but the various other flags that get enabled during the activation of various optimizations.</p><h2>Tradeoffs!</h2><p>When we need something, we need to lose something. In this context, if we need to get accurate results, then it&#8217;s recommended practice to use the defaults. The reason is numerous flags get enabled and difficult to understand the root cause of the failure. Or else you can turn on the desired optimization level, which could have a greater compilation time but faster execution time.</p><p>You can find the <a href="https://godbolt.org/z/E54exh6G5">code</a> here with the generated assembly without any installation. Also, you can play with all flags by enabling the respective flags like -O3, etc, in the right panel on the placeholder named `Compiler options&#8230;`<br><br>Stay <a href="https://sriniketh.design">connected</a> for more such content &#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Generators in Python]]></title><description><![CDATA[yield]]></description><link>https://srini047.substack.com/p/generators-in-python</link><guid isPermaLink="false">https://srini047.substack.com/p/generators-in-python</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Wed, 17 Jul 2024 17:16:10 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!-kat!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The `<a href="https://docs.python.org/3/reference/simple_stmts.html#yield">yield</a>` statement returns a <a href="https://docs.python.org/3/glossary.html#term-generator">generator</a> object. It can be used to fetch the only either individually or a bunch. It has a close correlation with a looping statement but behaves slightly differently.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-kat!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-kat!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!-kat!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!-kat!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!-kat!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-kat!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:31733,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-kat!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!-kat!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!-kat!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!-kat!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3012881f-101f-4c2e-a0fa-f593cbcc18cb_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Analogy to Looping Statements</h2><p>In looping statements, when the execution flow enters the loop, it will be executed until it ends and the flow comes out of the loop body.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>But say we need to iterate over the loop but need the flexibility to call each iteration only when required. This is where the yield statement comes into the picture. Whenever a function has a `yield` statement it has to be used inside a function and we call that a generator function that returns the generator object.</p><p>We can call the `<a href="https://docs.python.org/3/library/functions.html#next">next()</a>` method on the generator object to yield the result. If we want to run the generator object as a loop, wrap the generator object with a list, or tuple, the rest will be handled by Python.</p><h2>Features</h2><p>Listed below are some of the many features and advantages of using the `yield`. A few of these are covered in the <a href="https://colab.research.google.com/drive/1psgG8o4s4ZWe-NUgqKSlw2dpIr12c_Ux?usp=sharing">code</a> snippet. The rest can be viewed in this <a href="https://youtu.be/tmeKsb2Fras?si=GEdGNo945tBpBgSX">video</a>.</p><ul><li><p>It can be used for file processing</p></li><li><p>Used in cases where the storage is limited</p></li><li><p>Lazy sequences and collatz</p></li><li><p>Generator comprehensions</p></li><li><p>Generator features</p></li><li><p>Send, throw, and close</p></li></ul><p><em><strong>P.S.</strong></em>: I have added a benchmark as well to show the difference between generic loop and yield based loop.<br><br>Stay <a href="https://sriniketh.design">connected</a> for more such content &#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Operator Walrus]]></title><description><![CDATA[:=]]></description><link>https://srini047.substack.com/p/operator-walrus</link><guid isPermaLink="false">https://srini047.substack.com/p/operator-walrus</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Sat, 13 Jul 2024 07:29:03 GMT</pubDate><enclosure url="https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Why such a name?</h2><p>From the title, it is clear that it relates to the Walrus. The `:` represents the eyes and the `=` represents the tusks. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 424w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 848w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 1272w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 1456w" sizes="100vw"><img src="https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" width="3212" height="2294" data-attrs="{&quot;src&quot;:&quot;https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2294,&quot;width&quot;:3212,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 424w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 848w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 1272w, https://images.unsplash.com/photo-1460899960812-f6ee1ecaf117?q=80&amp;w=3212&amp;auto=format&amp;fit=crop&amp;ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Credits: Photo by <a href="https://unsplash.com/@wolsenburg?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Jay Ruzesky</a> on <a href="https://unsplash.com/photos/sea-lion-on-seashore-during-daytime-h13Y8vyIXNU?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure></div><p>This is more about the use case rather than explaining through content. The real zeal comes when implementing it in real life scenarios.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>About</h2><ul><li><p>Representation: `:=`</p></li><li><p>This assigns values to variables as part of functions, list comprehension, etc.</p></li><li><p>Available from Python &gt;= 3.8</p></li><li><p>Used to reduce complexity and improve readability.</p></li></ul><h2>Use cases</h2><ul><li><p>Assignment</p></li><li><p>In-built</p></li><li><p>User-defined</p></li><li><p>List comprehension</p></li></ul><p>Code: <a href="https://colab.research.google.com/drive/1Cm3li01GVBIQhNwWzXQdIGXn_s7Zpr-f?usp=sharing">https://colab.research.google.com/drive/1Cm3li01GVBIQhNwWzXQdIGXn_s7Zpr-f?usp=sharing</a></p><p></p><p>Please stay <a href="https://sriniketh.design">connected</a> for more such content&#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[chmod: Change the properties via CLI]]></title><description><![CDATA[-ugorwx]]></description><link>https://srini047.substack.com/p/chmod-change-the-properties-via-cli</link><guid isPermaLink="false">https://srini047.substack.com/p/chmod-change-the-properties-via-cli</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Thu, 11 Jul 2024 16:40:56 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Wv20!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wv20!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wv20!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wv20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:38456,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Wv20!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!Wv20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F162c6422-b611-4ebf-bd8b-2902c2fb78d7_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>File/directory permissions need to be carefully determined to provide the right level of access to the right users. In that sense, the command `chmod` comes into the picture which help us to provide appropriate access.</p><p><em>Starting with different user roles:</em></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><ul><li><p>(U) User - Manager i.e. with root user permission and complete control over the system.</p></li><li><p>(G) Group - Team i.e. comes under the owner with certain restricted access.</p></li><li><p>(O) Other - 3rd party i.e. everyone who isn&#8217;t part of owner/group.</p></li></ul><p><em>Different file permissions:</em></p><ul><li><p>(R) Read - Just to view the contents of the script.</p></li><li><p>(W) Write - Add content to the script.</p></li><li><p>(X) Execute - Provide run access to the script.</p></li></ul><p><em>Different file types:</em></p><ul><li><p>(-) File</p></li><li><p>(d) Represents a directory</p></li></ul><p><strong>Note</strong>: (- `hyphen`) refers to no particular file permission for that specific group except the first position i.e. file type.</p><p>An example of a default file permission that gets created if you are a root user for a file named `temp.txt`.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lgAF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lgAF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 424w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 848w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 1272w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lgAF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic" width="1456" height="411" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:411,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:34527,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lgAF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 424w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 848w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 1272w, https://substackcdn.com/image/fetch/$s_!lgAF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3a53f3e7-cf5a-4dab-85da-1f6826f57e14_2112x596.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>`777`</h2><p>If you feel allergic to alphabets then we can move to numericals. The equivalent of what is being shown in the alphabet. This table summarizes the values for all user levels and permissions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uWYU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uWYU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 424w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 848w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 1272w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uWYU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic" width="1136" height="1052" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1052,&quot;width&quot;:1136,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:42820,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uWYU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 424w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 848w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 1272w, https://substackcdn.com/image/fetch/$s_!uWYU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8460538d-b14e-42dc-8a4d-8fba5dc733bd_1136x1052.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So for example, if you want to grant complete access to a particular file then we can use the command:</p><pre><code>chmod 777 &lt;file.extension&gt;</code></pre><p>The value is derived by summing all the values which add up to `777` that&#8217;s its significance. If you want to provide just read access to the owner you can use the value `400`.</p><p>If you are still lazy then this online tool does the work job for you: <strong>https://chmod-calculator.com</strong>.</p><p>Stay <a href="https://sriniketh.design">connected</a> for more such content.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[GIL - Global Interpreter Lock]]></title><description><![CDATA[Note: We can perform multiprocessing in Python]]></description><link>https://srini047.substack.com/p/gil-global-interpreter-lock</link><guid isPermaLink="false">https://srini047.substack.com/p/gil-global-interpreter-lock</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Mon, 08 Jul 2024 19:09:10 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!694u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!694u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!694u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!694u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!694u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!694u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!694u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:19169,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!694u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!694u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!694u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!694u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e92fb0-33c5-4694-9e7e-2d1443b93207_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>What is <a href="https://docs.python.org/3/glossary.html#term-global-interpreter-lock">GIL</a>?</h2><p>The Python interpreter built in <a href="https://docs.python.org/3/glossary.html#term-CPython">CPython</a> ensures each Python byte code uses only one <a href="https://en.wikipedia.org/wiki/Thread_(computing)">thread</a>/process at a time thus locking the interpreter from accessing free resources for faster computation. Hence the term GIL.</p><h2>Why GIL?</h2><p>There has been much conversation around this topic even by <a href="https://youtu.be/m4zDBk0zAUY?si=yieZZst1NTj4BBc2">Guido Van Rossum</a>, the creator of Python Programming. The primary reason for GIL is to restrict concurrent access to the CPython interpreter. The reason for the restriction is CPython interpreter gives users the freedom to run multiple interpreters in the same process. This leads to shared resources and leads to unexpected bugs in the future.</p><h2>Then how to run concurrently?</h2><p>Though we have such constraints we have the flexibility to run concurrently by running them as subprocesses using the `<a href="https://docs.python.org/3/library/multiprocessing.html">multiprocessing</a>`. So we are bypassing multiple threads by using the strength of multiprocessors. This is independent of POSIX and Windows.</p><p>This comes with complex issues and features. One such is considering the fact there is a subprocess that&#8217;s consuming a lot of time and doesn&#8217;t complete the execution. So if we have the process as a <a href="https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon">daemon</a>, if the main program exits then it doesn&#8217;t depend on the completion of the sub-processes. This introduces unexpected behavior in Python but we need to lose something to gain something.</p><h2>Benchmark</h2><p>Now that we have seen the modules needed, we would see a simple <a href="https://colab.research.google.com/drive/1DWShJ8pDdVEcf5pFN5kWTGYigkwzXmWk?usp=sharing">implementation</a> of calculating the sum of first `n` natural numbers and know the difference between the execution times of various methods. Attaching the results of multiplying the first 1 lakh natural numbers with 10 threads using the basic Google colab configuration.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YFQd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YFQd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 424w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 848w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 1272w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YFQd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic" width="1240" height="1372" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1372,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:122084,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YFQd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 424w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 848w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 1272w, https://substackcdn.com/image/fetch/$s_!YFQd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b2db34-ba57-4a1a-8307-f8817c0c8a2d_1240x1372.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>On varying the threads in the power of 2 we can find the optimal number of threads.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!C4ZP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!C4ZP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 424w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 848w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 1272w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!C4ZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic" width="1456" height="844" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:844,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114506,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!C4ZP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 424w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 848w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 1272w, https://substackcdn.com/image/fetch/$s_!C4ZP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F064d51b7-6da8-41b0-bdb6-d113cce28a0e_1826x1058.heic 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Miscellaneous</h2><p>Besides the built-in features, we can induce multiprocessing in the following ways:</p><ul><li><p><a href="https://docs.python.org/3/library/ctypes.html">ctypes</a>: An interlink between C and Python that allows calling the DLL or `.so` files. Since C supports multi-threading it gives the freedom to run the code in multiple threads but we need to be careful and be prepared for the behavior.</p></li><li><p><a href="https://pybind11.readthedocs.io/en/stable/">pybind</a>: A C++ and Python interlink similar to `ctypes` but mainly to create Python bindings for existing C++ code.</p></li><li><p>Using <a href="https://numpy.org/doc/stable/benchmarking.html">numpy</a>, <a href="https://jax.readthedocs.io/en/latest/jit-compilation.html">jit</a>, <a href="https://numba.pydata.org">numba</a>, and more can speed up the execution without using multithreading.</p></li></ul><p><strong>Note</strong>:</p><ul><li><p>They both seem similar but the most difficult part is dealing with memory mapping and resource management. Eg: C++ has `int` of say 16 bits, but in Python, everything is an object. So ensuring proper resource management is the key here.`</p></li><li><p>There has been a <a href="https://peps.python.org/pep-0703/">proposal</a> to disable GIL but this proposal can go both ways as mentioned.</p></li></ul><p><strong>References</strong>: <a href="https://peps.python.org/pep-0683/">PEP-683</a>, <a href="https://peps.python.org/pep-0684/">PEP-684</a>, <a href="https://peps.python.org/pep-0703/">PEP-703</a></p><p>Code: <a href="https://colab.research.google.com/drive/1DWShJ8pDdVEcf5pFN5kWTGYigkwzXmWk?usp=sharing">https://colab.research.google.com/drive/1DWShJ8pDdVEcf5pFN5kWTGYigkwzXmWk?usp=sharing</a></p><p></p><p>Stay <a href="https://sriniketh.design">connected</a> for more content &#129309;<br>P.S.: Do let me know if there is any bias or mistakes. Happy to correct them going forward.</p>]]></content:encoded></item><item><title><![CDATA[Reservoir Sampling]]></title><description><![CDATA[A commonly used production sampling technique]]></description><link>https://srini047.substack.com/p/reservoir-sampling</link><guid isPermaLink="false">https://srini047.substack.com/p/reservoir-sampling</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Thu, 04 Jul 2024 20:08:26 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!iUzy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iUzy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iUzy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 424w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 848w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 1272w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iUzy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic" width="1456" height="521" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:521,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45353,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iUzy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 424w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 848w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 1272w, https://substackcdn.com/image/fetch/$s_!iUzy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1dc82e-f318-4caf-9286-f8ef23486c89_2348x840.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In a production environment where the data is being <a href="https://aws.amazon.com/what-is/streaming-data/">streamed</a>, it is indefinite (doesn&#8217;t fit the memory), and the condition arises where we would need to get random samples of size `k` with equal probability at any point in time. This concept is called <a href="https://en.wikipedia.org/wiki/Reservoir_sampling">Reservoir</a> sampling and comes in handy when the memory space is limited but at the same time, we have lots of data incoming from multiple streams like Kafka, Kinesis, etc.</p><p>To be precise equal probability <em><strong>(k / n)</strong></em> means from the reservoir of length `k` at any point of time where the length of current streamed data is `n` then for the `a[i]` element its probability is defined as:</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="pullquote"><ul><li><p>P(a[i] element) = P(a[i] element getting chosen) * P(not getting replaced by a[i+1] element) * P(not getting replaced by a[i+2] element) * &#8230;</p></li><li><p>P(nth element getting chosen) = k / i</p></li><li><p>P(not getting replaced by a[i+1] element) = k / i + 1</p></li><li><p>P(not getting replaced by a[i+2] element) = k / i + 2</p></li></ul></div><p>If we keep multiplying the values all we are left with is called the equal probability.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E2Bu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E2Bu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 424w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 848w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 1272w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E2Bu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic" width="1308" height="330" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:330,&quot;width&quot;:1308,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25675,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E2Bu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 424w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 848w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 1272w, https://substackcdn.com/image/fetch/$s_!E2Bu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F90dd4f77-16a5-45ae-9972-1924b025f564_1308x330.heic 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Credits: From Designing Machine Learning Systems, An Iterative Process for Production-Ready Applications, Pg:87, Fig:4-2</figcaption></figure></div><p>In programming terms consider a reservoir(array) of fixed length `k`. When `n` is less than `k` then well and good.</p><p>If not then generate a random index between 1 to n. If that lies between 1 and k, then replace it with the nth element. Or else continue to the next element in the stream. The pseudocode for the same can be found here.</p><pre><code>int arr[k];  # consider 1-based indexing

while (stream):
    if (n &lt; k)
        # Then add to the reservoir
    else
        i = generate_random(n) # range of `i`: [1,k]
        if (i &lt; k)
            # Then replace the reservoir element: arr[i]=nth_element
        else
            # do nothing
        fi
    fi
end loop</code></pre><p>A problem with a similar approach can be found <a href="https://leetcode.com/problems/linked-list-random-node/">here</a>.</p><p>Stay <a href="https://leetcode.com/problems/linked-list-random-node/">connected</a> for more such content &#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Instance, Class, and Static Methods]]></title><description><![CDATA[Types of methods in Python]]></description><link>https://srini047.substack.com/p/instance-class-and-static-methods</link><guid isPermaLink="false">https://srini047.substack.com/p/instance-class-and-static-methods</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Wed, 03 Jul 2024 15:57:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!JGWn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JGWn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JGWn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JGWn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32815,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JGWn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!JGWn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc60e0a87-ec5c-4d19-8c49-495fa20384a1_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>When we are working with classes in Python or any modern programming language that supports <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">OOPS</a>, then we more often come across the terms: instance, class, and static methods. Let&#8217;s understand the differences by the end.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Now let&#8217;s use a base picture of a class named `Employee` with basic details and it has other methods with its individual use cases.</p><pre><code>class Employee:
    num_of_employees = 0

    # Instance Method
    def __init__(self, id: int, name: str, salary: float):
        self.id = id
        self.name = name
        self.salary =salary
        Employee.num_of_employees += 1

    # Instance Method
    def display_employee_details(self) -&gt; str:
        return f"Employee with ID: {self.id} called as {self.name}, has a salary of {self.salary}"

    @classmethod
    def count_of_employees(cls):
        print(f"Number of employees: {cls.num_of_employees}")

    @staticmethod
    def is_valid(id: int) -&gt; bool:
        if (id &lt; 0 or id &gt; 99):
            return False
        else:
            return True</code></pre><p>Let&#8217;s use this code as a base and proceed further.</p><p><em>P.S.</em>: Avoid the logic for validation and print statements&#128517;</p><h2>Instance Method</h2><p>Any method that has the current instance of the class as its argument then we can call that as an <strong>Instance Method</strong>. They are contained and can be used to manipulate the values for a particular instance of the class where changes in one instance don&#8217;t affect the others. They are the first argument of the method and are not passed as parameters explicitly. We can give it any name but the common naming convention is `self`.</p><h2>Class Method</h2><p>They are limited to the class and have access to the class itself and have access to the nearest parent class. It is wrapped using the `<a href="https://docs.python.org/3/library/functions.html#classmethod">@classmethod</a>` <a href="https://docs.python.org/3/glossary.html#term-decorator">decorator</a> in Python. When we have a class derived from the base class, then the `@classmethod` of the derived class accesses the derived class method.</p><blockquote><p>If a class method is called for a derived class, the derived class object is passed as the implied first argument.</p><p>~ From the official Python docs</p></blockquote><h2>Static Method</h2><p>They are associated with a class rather than an instance. This is useful when we need access to a function from the class without using its instance. This method is wrapped using the `<a href="https://docs.python.org/3/library/functions.html#staticmethod">@staticmethod</a>` decorator as against using the `static` keyword in C++ and Java.</p><h3>Why the @staticmethod then?</h3><p>If we can access the method without instance then we can declare it globally and access it. Let&#8217;s consider the `<code>isValidId` method is present inside the `Employee` class because it has its meaning to check if the current employee id is valid. If we have it globally and give it a more meaningful name like `is_valid_employee` without modifying the logic does the job. But the real zeal of OOPs is not consumed hence it is recommended to use the `@staticmethod` where and when applicable with appropriate tradeoffs.</code></p><p>Code with executable cases to understand the working: <a href="https://colab.research.google.com/drive/1gti1ksYICLurvVYEzduI2UNGptXjuh2d?usp=sharing">https://colab.research.google.com/drive/1gti1ksYICLurvVYEzduI2UNGptXjuh2d?usp=sharing</a></p><p>Stay <a href="https://sriniketh.design">connected</a> for more content &#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[0.1 + 0.2 != 0.3]]></title><description><![CDATA[Whaaaat&#128561; Why&#129300;]]></description><link>https://srini047.substack.com/p/01-02-03</link><guid isPermaLink="false">https://srini047.substack.com/p/01-02-03</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Tue, 02 Jul 2024 18:36:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!bJ1X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bJ1X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bJ1X!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bJ1X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36577,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bJ1X!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 424w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 848w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 1272w, https://substackcdn.com/image/fetch/$s_!bJ1X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1227df27-1830-464e-9f8b-f05c1c54f290_600x600.heic 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Understanding the Problem</h2><p>Now let&#8217;s consider the case of adding 0.1 and 0.2. The answer is 0.3. This is the output from the calculator. Pretty straightforward, but let&#8217;s jump to the twist.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IEM0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IEM0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 424w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 848w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 1272w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IEM0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic" width="134" height="116" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:116,&quot;width&quot;:134,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6295,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IEM0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 424w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 848w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 1272w, https://substackcdn.com/image/fetch/$s_!IEM0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8c2e0fa-e1cd-4f0c-8510-afd8543f0081_134x116.heic 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>Then what is so special about this lies in its actual working in most of the programming languages. For simplicity let&#8217;s consider Python. This is the output when the code is executed on a Python interpreter.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!geOH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!geOH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 424w, https://substackcdn.com/image/fetch/$s_!geOH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 848w, https://substackcdn.com/image/fetch/$s_!geOH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 1272w, https://substackcdn.com/image/fetch/$s_!geOH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!geOH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic" width="342" height="164" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:164,&quot;width&quot;:342,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9875,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!geOH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 424w, https://substackcdn.com/image/fetch/$s_!geOH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 848w, https://substackcdn.com/image/fetch/$s_!geOH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 1272w, https://substackcdn.com/image/fetch/$s_!geOH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F215261ee-e8a7-4b90-a543-6fec854b5c1f_342x164.heic 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>Pause for a second to analyze the root cause. Now let&#8217;s understand what could be the actual reason.</p><h2>Root cause</h2><p>We can conclude that the sum of 0.1 and 0.2 is not 0.3. Then what is the value of the same? So let&#8217;s execute and find it out.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DRqn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DRqn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 424w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 848w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 1272w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DRqn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic" width="406" height="90" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:90,&quot;width&quot;:406,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12788,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/heic&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DRqn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 424w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 848w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 1272w, https://substackcdn.com/image/fetch/$s_!DRqn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383bc1d7-febd-43be-b893-b5183c4386ad_406x90.heic 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>What&#8217;s this weird number&#128565;&#8205;&#128171;? Now let&#8217;s dig deep into what could have led to this.</p><p>In Python, floating point numbers are represented as 32 or 64-bit values based on the compiler using the <a href="https://ieeexplore.ieee.org/document/8766229">IEEE-754</a> standard. So now let&#8217;s convert all the values into their respective binary.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qQg-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qQg-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 424w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 848w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 1272w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qQg-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png" width="461" height="98" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:98,&quot;width&quot;:461,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10608,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qQg-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 424w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 848w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 1272w, https://substackcdn.com/image/fetch/$s_!qQg-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6384fd5-fc9f-4985-b6c6-3a7201fdf090_461x98.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">sign(1), exponent(8), mantissa(23) &amp;&amp; ans is the weird number, see colab for implementation</figcaption></figure></div><p>From this, one can observe that the sum of 0.1 and 0.2 equate to `ans` and this is not as expected but happens internally which is the actual reason for its evaluation to be false.<br>The repeated mantissa values cause overflow during addition thereby leading to round-off and resulting in decimation.</p><p>Code: <a href="https://colab.research.google.com/drive/1tgHYBz0kWZ96weMa1dIuGx2Jiyo6OFGX?usp=sharing">https://colab.research.google.com/drive/1tgHYBz0kWZ96weMa1dIuGx2Jiyo6OFGX?usp=sharing</a></p><p>Stay connected for more content &#129309;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://srini047.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Engineering Dosage! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[First Draft]]></title><description><![CDATA[Highlights the purpose of the newsletter and a bit about myself.]]></description><link>https://srini047.substack.com/p/first-draft</link><guid isPermaLink="false">https://srini047.substack.com/p/first-draft</guid><dc:creator><![CDATA[Sriniketh]]></dc:creator><pubDate>Tue, 02 Jul 2024 15:27:15 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/c8eebdc7-c86a-4a36-a515-75121aaced28_420x300.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi folks,<br>This newsletter primarily focuses on producing quality content along the lines of software development, ML, Open source, technical content writing, and more. I would be regular(one each. a day probably) in publishing because translating my thoughts into words, they stay for a long time and better express the issue or concept.</p><p>In the current tech world, all the content that prevails is short-span and forgotten more often. Hence there must be a solution to persist the learning that will be useful in the long run. Hence this idea of a newsletter erupted.</p><h2>Why Engineering Dosage?</h2><p>A simple fact is that I&#8217;m from an Engineering background and we consume information byte-sized like medicine that either provides relief from a problem (issue) or improves the current state of your body (concept). All medicines are prescribed in dosage hence the name the <strong>Engineering Dosage</strong>.</p><h2>About me</h2><p>I&#8217;m Sriniketh J, a passionate software and ML developer enthusiastic about the latest advancements. Always keen to learn and be curious. I always like to upskill myself. Ask me about open-source, AI-ML, software development, or anything in general. Always willing to collaborate and contribute to the community. I'm inculcating the habit of reading books. I like to play f&#252;&#223;all a lot and learning deutsch as well.</p><p>P.S.: I had been having this idea about a year ago but it just stayed in my to-do list. This time I would make it live and be consistent.</p><p>Know more about me here: <a href="https://sriniketh.design/">https://sriniketh.design/</a></p><p>Support my work: <a href="https://www.buymeacoffee.com/srini047">https://www.buymeacoffee.com/srini047</a><br><br>Cheers,</p><p>-Sriniketh J.</p>]]></content:encoded></item></channel></rss>