<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marc Katz</title>
    <description>The latest articles on DEV Community by Marc Katz (@marckatz).</description>
    <link>https://dev.to/marckatz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110082%2F39f5edf3-1612-4ddd-adfc-102ed7cdf94a.png</url>
      <title>DEV Community: Marc Katz</title>
      <link>https://dev.to/marckatz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marckatz"/>
    <language>en</language>
    <item>
      <title>Some basic bash tips</title>
      <dc:creator>Marc Katz</dc:creator>
      <pubDate>Thu, 31 Aug 2023 02:45:20 +0000</pubDate>
      <link>https://dev.to/marckatz/some-basic-bash-tips-4b02</link>
      <guid>https://dev.to/marckatz/some-basic-bash-tips-4b02</guid>
      <description>&lt;p&gt;When using a Unix terminal, executing commands can be complicated and time consuming to do manually. However, with the power of bash scripting they can all be automated and simplified. Here are a few basic tips you can use to spice up your command line experience:&lt;/p&gt;

&lt;h2&gt;
  
  
  Making a bash script
&lt;/h2&gt;

&lt;p&gt;The first thing to do is to create a script file. This is a file with the suffix &lt;code&gt;.sh&lt;/code&gt;. These files can be run from the command line with the command &lt;code&gt;bash &amp;lt;filename.sh&amp;gt;&lt;/code&gt;. To the start of the file, add &lt;code&gt;#!/bin/bash&lt;/code&gt; to tell the terminal how to run it. &lt;br&gt;
In this file, you can do anything else you can in the command line, such as &lt;code&gt;ls&lt;/code&gt; (list files in the directory), &lt;code&gt;echo&lt;/code&gt; (print to the terminal), &lt;code&gt;cd&lt;/code&gt; (change directory), and &lt;code&gt;cat&lt;/code&gt; (write to file). &lt;/p&gt;
&lt;h2&gt;
  
  
  Variables and input
&lt;/h2&gt;

&lt;p&gt;Like in other languages, you can create variables in a bash script. It's extremely simple, just &lt;code&gt;&amp;lt;variable name&amp;gt; = &amp;lt;value&amp;gt;&lt;/code&gt;. If you want to access the variable later on, you can do so with &lt;code&gt;$&amp;lt;variable name&amp;gt;&lt;/code&gt;. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;read user_input
echo $user_input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will simply print whatever the user typed into the command line back out at them.&lt;br&gt;
For input, there are two main waits you can get it. One way is with &lt;code&gt;read&lt;/code&gt;, which takes in input typed by the user in the command line. The other is with arguments, which can be passed when running the script initially. To access the arguments, use &lt;code&gt;$&amp;lt;n&amp;gt;&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the nth argument passed. &lt;/p&gt;
&lt;h2&gt;
  
  
  Basic logic
&lt;/h2&gt;

&lt;p&gt;The next step to make these scripts more programmable is with logic. The first main piece of logic is with if/elif/else statements. The main syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [[&amp;lt;condition&amp;gt;]]; then
    &amp;lt;code&amp;gt;
elif [[&amp;lt;another condition&amp;gt;]]; then
    &amp;lt;code&amp;gt;
else
    &amp;lt;code&amp;gt;
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to add &lt;code&gt;fi&lt;/code&gt; to the end to end the if statement! You can also add as my &lt;code&gt;elif&lt;/code&gt; statements as you need.&lt;br&gt;
For the conditions, some basic logic operators include &lt;code&gt;-a &amp;lt;filename&amp;gt;&lt;/code&gt; (returns true if the file exists), &lt;code&gt;-s &amp;lt;filename&amp;gt;&lt;/code&gt; (returns true if the file exists and isn't empty), and &lt;code&gt;-z &amp;lt;string&amp;gt;&lt;/code&gt; (returns true if the string is empty, &lt;code&gt;-n&lt;/code&gt; is the opposite). For &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, and &lt;code&gt;not&lt;/code&gt;, you can use &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;/&lt;code&gt;-a&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt;/&lt;code&gt;-o&lt;/code&gt;, and &lt;code&gt;!&lt;/code&gt; respectively. &lt;/p&gt;
&lt;h2&gt;
  
  
  Loops
&lt;/h2&gt;

&lt;p&gt;Lastly, we can add loops to the script. For a basic &lt;code&gt;while&lt;/code&gt; loop, write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while [[&amp;lt;condition&amp;gt;]]; do
   &amp;lt;code&amp;gt;
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a for loops, do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for &amp;lt;var&amp;gt; in {&amp;lt;start&amp;gt;..&amp;lt;end&amp;gt;} do
   &amp;lt;code&amp;gt;
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute &lt;code&gt;&amp;lt;code&amp;gt;&lt;/code&gt; &lt;code&gt;&amp;lt;end&amp;gt;-&amp;lt;start&amp;gt;&lt;/code&gt; times, with the value stored in &lt;code&gt;&amp;lt;var&amp;gt;&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Finding Differences Two Ways: Bash and Diff</title>
      <dc:creator>Marc Katz</dc:creator>
      <pubDate>Wed, 09 Aug 2023 14:36:18 +0000</pubDate>
      <link>https://dev.to/marckatz/finding-differences-two-ways-bash-and-diff-3da3</link>
      <guid>https://dev.to/marckatz/finding-differences-two-ways-bash-and-diff-3da3</guid>
      <description>&lt;p&gt;For many projects, it is important to be able to figure out the changes that were made between two files or objects. This can be if you're recording edits, making incremental changes, or working collaboratively. In this post, I will describe how to do this through the Bash shell and with the difflib python library.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Bash diff Function&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the bash shell, there is a function &lt;code&gt;diff&lt;/code&gt; that can directly compare two files (or directories). The basic format is simple, &lt;code&gt;diff &amp;lt;file1&amp;gt; &amp;lt;file1&amp;gt;&lt;/code&gt;. This will output a string called a "diff" or "patch" describing what changes to make to turn &lt;code&gt;file1&lt;/code&gt; into &lt;code&gt;file2&lt;/code&gt;. (Sidenote: if the files are identical, there will be no output, but if the files are not text files, it will output &lt;code&gt;0&lt;/code&gt; if they are identical and &lt;code&gt;1&lt;/code&gt; if they are not). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Options&lt;/strong&gt;&lt;br&gt;
There are many useful options to add to the &lt;code&gt;diff&lt;/code&gt; command depending on what you need. Some include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--ignore-space-change&lt;/code&gt;: ignore changes in whitespace&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--ignore-blank-lines&lt;/code&gt;: ignore lines that are blank&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--ignore-case&lt;/code&gt;: ignore changes in case&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--minimal&lt;/code&gt;: "Try hard to find a smaller set of changes" - from the Man page&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--recursive&lt;/code&gt;: when diffing directories, search through their sub-directories recursively &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--side-by-side&lt;/code&gt;: output the changes in two columns, makes it much more human-readable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--suppress-common-lines&lt;/code&gt;: don't output lines that are identical&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reading the output&lt;/strong&gt;&lt;br&gt;
Let's use the following example: You have two text files, &lt;code&gt;a.txt&lt;/code&gt; and &lt;code&gt;b.txt&lt;/code&gt;. &lt;br&gt;
&lt;code&gt;a.txt&lt;/code&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is line 1
This is line 2
This is line 3
This is line 4

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;b.txt&lt;/code&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is a new line
This is line 1
This is line 2 modified
This is line 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the following command: &lt;code&gt;diff --minimal a.txt b.txt&lt;/code&gt;, we get the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0a1
&amp;gt; This is a new line
2c3
&amp;lt; This is line 2
---
&amp;gt; This is line 2 modified
4d4
&amp;lt; This is line 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three sections of this output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0a1
&amp;gt; This is a new line
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;a&lt;/code&gt; in the first line means that you have to add the following lines. In this case, &lt;code&gt;0a1&lt;/code&gt; means you add line 1 from &lt;code&gt;b.txt&lt;/code&gt; to line 0 (the start of the file) to &lt;code&gt;a.txt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2c3
&amp;lt; This is line 2
---
&amp;gt; This is line 2 modified
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;c&lt;/code&gt; means that this is a change. Here, line 2 in &lt;code&gt;a.txt&lt;/code&gt; is replaced by line 3 in &lt;code&gt;b.txt&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4d4
&amp;lt; This is line 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;d&lt;/code&gt; means that there is a delete. Here, line 4 in &lt;code&gt;a.txt&lt;/code&gt; is deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  difflib Python Library
&lt;/h2&gt;

&lt;p&gt;However, if you want to do this in your code, you can't just use the bash shell. In Python, one way to do this is to use the &lt;code&gt;difflib&lt;/code&gt; library. &lt;code&gt;difflib&lt;/code&gt; is part of the Python Standard Library, so you don't need to install anything, just add &lt;code&gt;import difflib&lt;/code&gt; to your file. There are many commands in this library, but the two we will go over is &lt;code&gt;unified_diff&lt;/code&gt; and &lt;code&gt;Differ.compare&lt;/code&gt;. We will be using these commands to compare two strings:&lt;br&gt;
&lt;code&gt;a&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is line 1
This is line 2
This is line 3
This is line 4

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;b&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This is a new line
This is bine 1
This is 2a
This is line 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that these strings end with a newline, this is important. If you want to compare two string that don't end in a newline, you will have to add it to use these functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;unified_diff&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;unified_diff&lt;/code&gt; is the simpler of the two commands, but does not give as much detail as the other one. The format is simple: &lt;code&gt;difflib.unified_diff(list1, list2)&lt;/code&gt;. The function takes in two list of strings, each ending in a new line. You can easily turn your string into a list like that by running &lt;code&gt;&amp;lt;string&amp;gt;.splintlines(keepends=True)&lt;/code&gt;. This function will output a "generator function", so you will have to turn it into a &lt;code&gt;list&lt;/code&gt;, or immediately iterate through it. For this example, we will use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;diffs = difflib.unified_diff(a.splitlines(keepends=True),b.splitlines(keepends=True))
for line in diffs:
    print(line,end='')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The &lt;code&gt;end=''&lt;/code&gt; is needed because each line of the output already ends in a newline)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reading the output&lt;/strong&gt;&lt;br&gt;
For the above example, the output will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--- 
+++
@@ -1,5 +1,5 @@
-This is line 1
-This is line 2
+This is a new line
+This is bine 1
+This is 2a
 This is line 3
-This is line 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To read this, the first part of describes the lines of the string we are looking at. Here, we are looking at lines 1 to 5 in both strings. Lines starting with &lt;code&gt;-&lt;/code&gt; will be deleted from the first string, and lines starting with &lt;code&gt;+&lt;/code&gt; will be added from the second string. As you can see, this is very compact, but not very detailed, as it only works with adding and deleting whole lines&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Differ.compare&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Differ.compare&lt;/code&gt; will give much more detailed results than &lt;code&gt;unified_diff&lt;/code&gt;. Like &lt;code&gt;unified_diff&lt;/code&gt;, you have to use &lt;code&gt;splitlines&lt;/code&gt; first, since it takes lists of strings. You also have to create a &lt;code&gt;Differ&lt;/code&gt; object, since it is a function of that class. For this example, we will use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;d = difflib.Differ()
results = d.compare(a.splitlines(keepends=True),b.splitlines(keepends=True))
for line in results:
    print(line,end='')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reading the output&lt;/strong&gt;&lt;br&gt;
For the above example, the output will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+ This is a new line
- This is line 1
?         ^
+ This is bine 1
?         ^
- This is line 2
?         -----
+ This is 2a
?          +
  This is line 3
- This is line 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like, &lt;code&gt;unified_diff&lt;/code&gt;, lines starting with &lt;code&gt;+&lt;/code&gt; are removed from the first string, and lines starting with &lt;code&gt;-&lt;/code&gt; are removed from the second string. However, we now have these &lt;code&gt;?&lt;/code&gt; lines. These lines further describe the changes within each lines. If there is a &lt;code&gt;^&lt;/code&gt; in that line, the character above the &lt;code&gt;^&lt;/code&gt; in the line above will be replaced by another character above a &lt;code&gt;^&lt;/code&gt; in the line below. So in the case above, the &lt;code&gt;l&lt;/code&gt; in &lt;code&gt;This is line 1&lt;/code&gt; is replaced by a &lt;code&gt;b&lt;/code&gt;. If there are &lt;code&gt;-&lt;/code&gt;s, that means those characters are deleted from the line. Here, &lt;code&gt;line&lt;/code&gt; is removed from &lt;code&gt;This is line 2&lt;/code&gt;. If there are &lt;code&gt;+&lt;/code&gt;s, that means the characters are added to the line. Here, an &lt;code&gt;a&lt;/code&gt; is added to the end of &lt;code&gt;This is line 2&lt;/code&gt;. &lt;br&gt;
As you can see, this is much more complicated than &lt;code&gt;unified_diff&lt;/code&gt;, but now you can see the exact changes you need to make to each line and where, instead of just completely removing and adding whole lines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://ss64.com/bash/diff.html"&gt;https://ss64.com/bash/diff.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.python.org/3/library/difflib.html#differ-objects"&gt;https://docs.python.org/3/library/difflib.html#differ-objects&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Basics of Animation in CSS: Rotating and Scaling</title>
      <dc:creator>Marc Katz</dc:creator>
      <pubDate>Wed, 26 Jul 2023 15:33:41 +0000</pubDate>
      <link>https://dev.to/marckatz/the-basics-of-animation-in-css-rotating-and-scaling-4i63</link>
      <guid>https://dev.to/marckatz/the-basics-of-animation-in-css-rotating-and-scaling-4i63</guid>
      <description>&lt;p&gt;Animations in websites can be done in many ways. From coding it in vanilla Javascript, to the countless libraries you can download, to Flash (rip), you can pick and choose what you use to fit your needs. But one basic way to do so is in CSS. This is useful since they are natively supported by all the major browsers and don't require any extra downloads. In this post, we will show how to rotate and scale in multiple dimensions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting the Stage
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;note: this will be done in SCSS&lt;/em&gt;&lt;br&gt;
First, we need to set up the objects we want to animate. We will create 3 axes, and a circle at the center, all as &lt;code&gt;div&lt;/code&gt;s. We'll wraps them in a single parent "scene" &lt;code&gt;div&lt;/code&gt;. This will be all the HTML we'll need for this. So far, our code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class='scene'&amp;gt;
  &amp;lt;div class="axis x"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div class="axis y"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div class="axis z"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;div class='circle'&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, if you look at this in the browser, you'll see nothing. So now, we need to add some basic properties to our &lt;code&gt;div&lt;/code&gt;s. &lt;br&gt;
For our &lt;code&gt;body&lt;/code&gt;, let's set the &lt;code&gt;background-color&lt;/code&gt; to black, &lt;code&gt;display&lt;/code&gt; to grid, &lt;code&gt;place-items&lt;/code&gt; to center, &lt;code&gt;min-height&lt;/code&gt; to 100vh (100% of the viewport size), and &lt;code&gt;overflow&lt;/code&gt; to hidden.&lt;br&gt;
For our &lt;code&gt;scene&lt;/code&gt; div, we just need to set the &lt;code&gt;position&lt;/code&gt; to relative.&lt;br&gt;
For our 3 &lt;code&gt;axis&lt;/code&gt; divs, we'll give them a &lt;code&gt;height&lt;/code&gt; of 1px (1 pixel), a &lt;code&gt;width&lt;/code&gt; of 200vh (twice the size of the viewport), a &lt;code&gt;left&lt;/code&gt; of -100vh (to center them), and &lt;code&gt;position&lt;/code&gt; to absolute. We'll also give each axis their own color, using the &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;z&lt;/code&gt; classes. We will also &lt;code&gt;rotate&lt;/code&gt; &lt;code&gt;y&lt;/code&gt; by 90deg around the z-axis, and the &lt;code&gt;z&lt;/code&gt; by 90deg around the y axis.&lt;br&gt;
Lastly, we'll give the &lt;code&gt;circle&lt;/code&gt; div a &lt;code&gt;position&lt;/code&gt; of absolute, &lt;code&gt;border-radius&lt;/code&gt; of 50% (which will make it a circle), a &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; of 120px, a &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;top&lt;/code&gt; of -60px (to center it), and a &lt;code&gt;background&lt;/code&gt; of white.&lt;br&gt;
So far, it should look like:&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/MarcKatz/nt2xecw7/2//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting the animations: Rotating the scene
&lt;/h2&gt;

&lt;p&gt;Let's start by animating the entire scene, so it is if we are orbiting the circle. First, we have to set the &lt;code&gt;perspective&lt;/code&gt; of the &lt;code&gt;body&lt;/code&gt; to 800px, so the "camera" will be 800px from the center. We will also add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  *:not(:empty){
    transform-style: preserve-3d;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to the &lt;code&gt;body&lt;/code&gt; which will allow us to see things in 3 dimensions.&lt;br&gt;
Next, let's add an initial rotation around the x-axis by adding &lt;code&gt;transform: rotateX(10deg);&lt;/code&gt;.&lt;br&gt;
Since we are rotating the whole scene, we will give the &lt;code&gt;animation&lt;/code&gt; to the &lt;code&gt;scene&lt;/code&gt; div. Our &lt;code&gt;animation&lt;/code&gt; will take 4 inputs: a name, a duration, an iteration number, and a timing function. Let's call this animation "orbit", have it last 20 seconds, repeat &lt;code&gt;infinite&lt;/code&gt;ly, and with a &lt;code&gt;linear&lt;/code&gt; timing function. So far, it should look like &lt;code&gt;animation: orbit 20s infinite linear;&lt;/code&gt;&lt;br&gt;
Next, we have to create the &lt;code&gt;keyframes&lt;/code&gt;. Each &lt;code&gt;keyframe&lt;/code&gt; will specify how far we want the rotation to go. Luckily, we only need two &lt;code&gt;keyframes&lt;/code&gt;. The first will be at 0%, where we will give it a &lt;code&gt;transform&lt;/code&gt; of &lt;code&gt;rotateY(0)&lt;/code&gt;. This is the starting position. At 100%, we want it to make a full rotation, so we can set another &lt;code&gt;keyframe&lt;/code&gt; at 100%, and give it a &lt;code&gt;transform&lt;/code&gt; of &lt;code&gt;rotateY(360deg)&lt;/code&gt;. We will also have to add the &lt;code&gt;rotateX(10deg)&lt;/code&gt; in each transform, in order to keep that constant.&lt;br&gt;
Now, the animation will look like:&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/MarcKatz/n47zhrLu/7//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotating the Circle
&lt;/h2&gt;

&lt;p&gt;Now, let's add an animation to the circle, so it rolls while clicked. First, we have to add the animation to the '.circle' div. Here, we will use &lt;code&gt;animation: roll 5s infinite linear;&lt;/code&gt;. We will also use very similar &lt;code&gt;keyframes&lt;/code&gt; as the scene, but with &lt;code&gt;rotateX&lt;/code&gt; instead of &lt;code&gt;rotateY&lt;/code&gt;. If you look at the code now, you'll see that the circle is rotating around the x-axis. However, we only want it to do so while clicked. So in &lt;code&gt;.circle.&lt;/code&gt;, we will add &lt;code&gt;animation-play-state: paused;&lt;/code&gt;. This will set the default state of the circle's animation to not run. Now, we'll add a new section, for &lt;code&gt;.circle:active&lt;/code&gt;. This will be applied while the &lt;code&gt;.circle&lt;/code&gt; div is being clicked. In here, we will add &lt;code&gt;animation-play-state: running;&lt;/code&gt;, making the animation run.&lt;br&gt;
Now, it will be:&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/MarcKatz/nxkmepv1/10//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Scaling the Circle
&lt;/h2&gt;

&lt;p&gt;Lastly, let's add another animation to the circle. &lt;br&gt;
Firstly, if we just add another animation, it will override the transitions that we already wrote. So let's first wrap our &lt;code&gt;circle&lt;/code&gt; div in a new &lt;code&gt;squish&lt;/code&gt; div.&lt;br&gt;
Next, we will make it stretch horizontally, then return to normal, then stretch vertically, and then return to normal. Again, we will have to add an animation to &lt;code&gt;.circle&lt;/code&gt;. We will add &lt;code&gt;, squish 20s infinite linear&lt;/code&gt; to our &lt;code&gt;animation&lt;/code&gt; property in &lt;code&gt;.circle&lt;/code&gt;. We will also have to add &lt;code&gt;, running&lt;/code&gt; to the &lt;code&gt;animation-play-state&lt;/code&gt; property, so it will run by default. Now, the &lt;code&gt;keyframes&lt;/code&gt; will be more difficult. First, we'll set the &lt;code&gt;keyframes&lt;/code&gt; that will be the default values. This will happen at 0%, 50%, and 100%. So we will add a &lt;code&gt;keyframe&lt;/code&gt; of &lt;code&gt;0%, 50%, 100% { transform: scale(1); }&lt;/code&gt;. Next, at 25%, we want to have a &lt;code&gt;transform&lt;/code&gt; of &lt;code&gt;scaleX(4)&lt;/code&gt;, which will make it four times as long. Finally, at 75%, we want a &lt;code&gt;transform&lt;/code&gt; of &lt;code&gt;scaleX(4)&lt;/code&gt; to make it 4 times as tall.&lt;br&gt;
Our final product will be:&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/MarcKatz/061xpu45/53//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Final thoughts
&lt;/h1&gt;

&lt;p&gt;Congrats! This is the first step towards understanding animations in CSS. There are many other operations you can do to make even more complicated animations, and there are some really impressive examples out there. Keep playing around with it make it even cooler!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Irregular Expressions: Matching Strings in Python</title>
      <dc:creator>Marc Katz</dc:creator>
      <pubDate>Mon, 03 Jul 2023 17:12:35 +0000</pubDate>
      <link>https://dev.to/marckatz/irregular-expressions-matching-strings-in-python-1iec</link>
      <guid>https://dev.to/marckatz/irregular-expressions-matching-strings-in-python-1iec</guid>
      <description>&lt;h2&gt;
  
  
  What is Regex?
&lt;/h2&gt;

&lt;p&gt;Regex, short for Regular Expressions, is a way to determine whether a string or a part of a string fits a certain pattern. It is very powerful and short, but the syntax is not very intuitive. In this article, I will give a brief run-down of how to use regex in python, and a basic dictionary for the symbols used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Startup
&lt;/h2&gt;

&lt;p&gt;In order to use regex in Python, you first need to import the module with &lt;code&gt;import re&lt;/code&gt;. Once you've done this, you're good to go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Search
&lt;/h2&gt;

&lt;p&gt;The first function that we'll go over is &lt;code&gt;search&lt;/code&gt;. This function takes in two parameters: the pattern that the function will look for, and the string it'll look for it in. The full syntax is &lt;code&gt;&amp;lt;match&amp;gt; = re.search(&amp;lt;pattern&amp;gt;, &amp;lt;string&amp;gt;)&lt;/code&gt;. If this function finds a match, it returns a &lt;code&gt;Match&lt;/code&gt; object representing the &lt;strong&gt;first&lt;/strong&gt; substring that fits the pattern, otherwise it return &lt;code&gt;None&lt;/code&gt;. You can get the matching substring with &lt;code&gt;&amp;lt;match&amp;gt;.group(0)&lt;/code&gt;, and the start and end positions of the substring as a tuple with &lt;code&gt;&amp;lt;match&amp;gt;.span()&lt;/code&gt;. For example, &lt;code&gt;re.search("ra", "abracadabra").span()&lt;/code&gt; will return &lt;code&gt;(2,4)&lt;/code&gt;. If you use groupings in your pattern (see below), you can use &lt;code&gt;&amp;lt;match&amp;gt;.group(&amp;lt;n&amp;gt;)&lt;/code&gt;, where n is the group number you want to access. &lt;/p&gt;

&lt;h2&gt;
  
  
  Find All
&lt;/h2&gt;

&lt;p&gt;Findall will give you all the substrings that match the pattern. &lt;code&gt;re.findall(&amp;lt;pattern&amp;gt;, &amp;lt;string&amp;gt;)&lt;/code&gt; will return a list of all the &lt;strong&gt;non-overlapping&lt;/strong&gt; substrings in the string that match the pattern. For example, &lt;code&gt;re.findall("a.{1,2}a", "abracadabra")&lt;/code&gt; will return &lt;code&gt;["abra", "ada"]&lt;/code&gt;. Note that it doesn't find &lt;code&gt;"aca"&lt;/code&gt; nor the second &lt;code&gt;"abra"&lt;/code&gt;, since they overlap with the substrings already found.&lt;/p&gt;

&lt;h2&gt;
  
  
  Split
&lt;/h2&gt;

&lt;p&gt;Split is used when you want to break up your original string. &lt;code&gt;re.split(&amp;lt;pattern&amp;gt;, &amp;lt;string&amp;gt;)&lt;/code&gt; will return an list of substrings, separated where the pattern matches in the string. For example, &lt;code&gt;re.split("ra", "abracadabra")&lt;/code&gt; will return &lt;code&gt;["ab", "cadab", ""]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Substitute
&lt;/h2&gt;

&lt;p&gt;Sub is used when you want to replace parts of your original string with something else. &lt;code&gt;re.sub(&amp;lt;pattern&amp;gt;, &amp;lt;replacement&amp;gt;, &amp;lt;string&amp;gt;)&lt;/code&gt; will replace all the substrings in &lt;code&gt;string&lt;/code&gt; that match the pattern with &lt;code&gt;replacement&lt;/code&gt;. For example, &lt;code&gt;re.sub("ra", "lo", "abracadabra")&lt;/code&gt; will return &lt;code&gt;"ablocadablo"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Special Characters
&lt;/h2&gt;

&lt;p&gt;There are many special characters that can be used in the pattern in order to make your searches more powerful. Here is a list of some of the more widely used ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt;: Start of the string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$&lt;/code&gt;: End of the string&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;[]&lt;/code&gt;: Will match any character inside the square braces. Ranges can be given with &lt;code&gt;-&lt;/code&gt;, and &lt;code&gt;^&lt;/code&gt; will negate it, matching anything except what's inside.&lt;/p&gt;

&lt;p&gt;Examples: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[abc]&lt;/code&gt; will match &lt;code&gt;"a"&lt;/code&gt;, &lt;code&gt;"b"&lt;/code&gt;, &lt;code&gt;"c"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^abc]&lt;/code&gt; will match anything except &lt;code&gt;"a"&lt;/code&gt;, &lt;code&gt;"b"&lt;/code&gt;, &lt;code&gt;"c"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[4-7f-e]&lt;/code&gt; will match any digit between 4 and 7, and any letter between f and e (inclusive)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.&lt;/code&gt;: Wildcard. This will match anything except a newline&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\d&lt;/code&gt;: Any digit. The same as &lt;code&gt;[0-9]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\D&lt;/code&gt;: Any non-digit. The same as &lt;code&gt;[^0-9]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\s&lt;/code&gt;: Any whitespace character, such as spaces, tabs, and newlines&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\S&lt;/code&gt;: Any non whitespace character&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\w&lt;/code&gt;: Any "word" character: numbers, letters, and _ (underscore)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\W&lt;/code&gt;: Any non-"word" character&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;*&lt;/code&gt;: Any number of repetitions of the expression before. For example, &lt;code&gt;a*b&lt;/code&gt; will match &lt;code&gt;"b"&lt;/code&gt;, &lt;code&gt;"ab"&lt;/code&gt;, &lt;code&gt;"aab"&lt;/code&gt;, &lt;code&gt;"aaab"&lt;/code&gt;, etc &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;+&lt;/code&gt;: One or more repetitions of the expression before. For example, &lt;code&gt;a+b&lt;/code&gt; will match &lt;code&gt;"ab"&lt;/code&gt;, &lt;code&gt;"aab"&lt;/code&gt;, &lt;code&gt;"aaab"&lt;/code&gt;, etc &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;?&lt;/code&gt;: One or no matches o the expression before. For example, &lt;code&gt;a?b&lt;/code&gt; will match &lt;code&gt;"b"&lt;/code&gt; and &lt;code&gt;"ab"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;{}&lt;/code&gt;: Used to match a specific number of repetitions: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{n}&lt;/code&gt;: will match exactly &lt;code&gt;n&lt;/code&gt; repetitions:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a{3}b&lt;/code&gt; will match &lt;code&gt;"aaab"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{n,}&lt;/code&gt;: will match &lt;code&gt;n&lt;/code&gt; or more repetitions:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a{3,}b&lt;/code&gt; will match &lt;code&gt;"aaab"&lt;/code&gt;, &lt;code&gt;"aaaab"&lt;/code&gt;, etc&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{n,m}&lt;/code&gt;: will match between &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;m&lt;/code&gt; repetitions, inclusive:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a{1,3}b&lt;/code&gt; will match &lt;code&gt;"ab"&lt;/code&gt;, &lt;code&gt;"aab"&lt;/code&gt;, and &lt;code&gt;"aaab"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;/code&amp;gt;: Will escape the next character, allowing you to search for special characters. For example &lt;code&gt;*\?&lt;/code&gt; will search for &lt;code&gt;"*?"&lt;/code&gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;|&lt;/code&gt;: "Or" function: will match either expression on each side. For example, &lt;code&gt;a|b&lt;/code&gt; will match &lt;code&gt;"a"&lt;/code&gt; and &lt;code&gt;"b"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;()&lt;/code&gt;: Will "group" the expression inside the parentheses, either for capturing with the functions above, or to use in relation with the repetition or | symbols. If you don't want to capture, use &lt;code&gt;(?:&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sources:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.python.org/3/library/re.html"&gt;https://docs.python.org/3/library/re.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/python/python_regex.asp"&gt;https://www.w3schools.com/python/python_regex.asp&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful Links:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://regex101.com/"&gt;https://regex101.com/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.rexegg.com/regex-quickstart.html"&gt;https://www.rexegg.com/regex-quickstart.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://xkcd.com/1313/"&gt;https://xkcd.com/1313/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://alf.nu/RegexGolf"&gt;https://alf.nu/RegexGolf&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Dark Magic, Evil Bits, and WTF</title>
      <dc:creator>Marc Katz</dc:creator>
      <pubDate>Wed, 28 Jun 2023 18:38:59 +0000</pubDate>
      <link>https://dev.to/marckatz/dark-magic-evil-bits-and-wtf-1cgm</link>
      <guid>https://dev.to/marckatz/dark-magic-evil-bits-and-wtf-1cgm</guid>
      <description>&lt;h2&gt;
  
  
  How Quake III devs sold their soul to the devil to save a few milliseconds
&lt;/h2&gt;

&lt;p&gt;Content warning: Math&lt;/p&gt;

&lt;p&gt;In video games, one of the most computationally intensive function is determining lighting. This is because when calculating light reflections in three dimensions, you need to find 1 divided by the square root of a number, or in other words, &lt;code&gt;1/sqrt(n)&lt;/code&gt;, its inverse square root. Since the number will be in floating-point format, division is computationally expensive. With modern computers, this effect is minimal, but back in the 90s, more thought had to be put into each machine cycle to get games to work.&lt;/p&gt;

&lt;p&gt;The developers working on Quake III Arena, released in 1999, came up with an ingenious solution to this problem. They found a way to calculate an inverse square root without division. Their code in C, with the original comments, was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float Q_rsqrt( float number )
{
 long i;
 float x2, y;
 const float threehalfs = 1.5F;

 x2 = number * 0.5F;
 y  = number;
 i  = * ( long * ) &amp;amp;y;                       // evil floating point bit level hacking
 i  = 0x5f3759df - ( i &amp;gt;&amp;gt; 1 );               // what the fuck?
 y  = * ( float * ) &amp;amp;i;
 y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
// y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

 return y;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Part 1: Evil Floating point bit level hacking
&lt;/h2&gt;

&lt;p&gt;So, this code is… complicated to say the least, and without knowing what exactly is going on, it is incomprehensible. Even if you know what each line does, why it works is still a long way away.&lt;/p&gt;

&lt;p&gt;Let’s start with the line &lt;code&gt;i = * ( long * ) &amp;amp;y&lt;/code&gt;;. As commented, this line is evil, in the sense that it breaks the rules. We take &lt;code&gt;y&lt;/code&gt;, which is a 32-bit floating-point number, and shove its bits into &lt;code&gt;i&lt;/code&gt;, which is a long (a 32-bit integer).&lt;/p&gt;

&lt;p&gt;In C, floats are stored in the following fashion: one bit for the sign, eight bits for the exponent (e), and 23 bits for the mantissa (m). If we look at the memory directly, &lt;code&gt;y&lt;/code&gt; is &lt;code&gt;0eeeeeeeemmmmmmmmmmmmmmm&lt;/code&gt; (side note: we know that the sign bit will be 0, denoting a positive number, since the square root of a negative number is imaginary). This means that &lt;code&gt;y = (1 + m) * 2 ^ e&lt;/code&gt;. Well, kinda. In order to have negative exponents, e is given an offset of 127 (1111111 in binary), so it actually is &lt;code&gt;y = (1 + m) * 2 ^ (e — 127)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So why make it a long? In order to do the following steps, we need to act on &lt;code&gt;y&lt;/code&gt;’s bits directly. It is easiest to do this as an integer, since C will now let us add and subtract without worrying about exponents and the like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: WTF
&lt;/h2&gt;

&lt;p&gt;Now we get to the next line: &lt;code&gt;i = 0x5f3759df — ( i &amp;gt;&amp;gt; 1 );&lt;/code&gt;, and as the developers so succinctly put it in the code’s comments, “what the fuck?”&lt;br&gt;
What are these arrows? What is that number? and how does any of this help us get a square root?&lt;/p&gt;

&lt;p&gt;Let’s start with the easiest part first: bit shifting. &lt;code&gt;i &amp;gt;&amp;gt; 1&lt;/code&gt; simply shifts the bits in i once to the right. Since i is a binary number, this is equivalent to dividing by 2. Now, our number looks like &lt;code&gt;00eeeeeeeemmmmmmmmmmmmmmm&lt;/code&gt;. If we view this back as a float, this is approximately equivalent to &lt;code&gt;(m/2) ^ (e/2)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But why do this? Well, we first have to reexamine what we are trying to do. Another way to view &lt;code&gt;1/sqrt(n)&lt;/code&gt; is &lt;code&gt;n^(-1/2)&lt;/code&gt;. So by bit-shifting right, we’ve (kinda) taken the square root. We negate it by subtracting our new number. But we still have to fix the errors that bit-shifting caused.&lt;/p&gt;

&lt;p&gt;The major potential source of error is last bit in the exponent, represented the bold e above, which now has found itself in the area of the mantissa. In order to fix this error, and some other issues (Such as halving the mantissa when we didn’t want to), the developers of Quake III found a Magic Number that helps alleviate these accumulated errors. This magic number is &lt;code&gt;0x5f3759df&lt;/code&gt;, or &lt;code&gt;1011111001101110101100111011111&lt;/code&gt; in binary, or &lt;code&gt;1597463007&lt;/code&gt; in decimal. How this number is derived is beyond the scope of this article, but it involves blood sacrifices and dark wizardry (or so I assume). If you want to learn more about this, read Dr. Lomont’s paper, linked below.&lt;/p&gt;

&lt;p&gt;And to put this all in a bow, we put our new, probably cursed, number back into a float with the line &lt;code&gt;y = * ( float * ) &amp;amp;i;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: Newton’s Method
&lt;/h2&gt;

&lt;p&gt;We’re almost at the finish line, we just need to make our guess (&lt;code&gt;y&lt;/code&gt;) more precise. The number we have is good, but it could be better. The way that the code does this is with one iteration of Newton’s method of approximation, &lt;code&gt;x_n+1 = x_n — f(x_n) / f’(x_n)&lt;/code&gt;, where &lt;code&gt;x_n&lt;/code&gt; is the current guess, &lt;code&gt;f(x)&lt;/code&gt; is the function we’re trying to approximate, and &lt;code&gt;f’(x)&lt;/code&gt; is its derivative. Since Newton’s method finds 0s, we need a function that we want to be 0. In this case, our function will be the error, so our &lt;code&gt;f(y) = 1/y²-x&lt;/code&gt;, where &lt;code&gt;x&lt;/code&gt; is the number we started with. This means its derivative is &lt;code&gt;f’(y) = -2/y³&lt;/code&gt;. Putting this together, we get &lt;code&gt;y — ( (1/y²-x) / (-2/y³) )&lt;/code&gt;, which simplifies to &lt;code&gt;y * (1.5 — x * y² / 2)&lt;/code&gt;. This gives us the final piece of the puzzle, &lt;code&gt;y = y * ( threehalfs — ( x2 * y * y ) );&lt;/code&gt;. Now, this method can be done multiple times to further make the it more accurate, but it seems the developers tried a second iteration and found it unnecessary.&lt;/p&gt;

&lt;p&gt;And finally, we have our Fast Inverse Square Root. All it took was some hacking and magic. But…&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 4: Was it worth it?
&lt;/h2&gt;

&lt;p&gt;Yes. This method is way faster than the built-in ways of solving for it, and it is very accurate. The maximum relative error is only 0.175%. But is it worth doing today? Modern computers can handle exponentially more computations than in the 90s, but games and simulations that would requires lots of these calculations have also become more demanding, complex, and exact. Whether or not you want to use a quick approximation like this or just whatever is native to your system will depend on the exact situation you are in and how much computing power you need to squeeze out of your user’s computers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sources:&lt;/strong&gt;&lt;br&gt;
&lt;a href="http://www.lomont.org/papers/2003/InvSqrt.pdf"&gt;http://www.lomont.org/papers/2003/InvSqrt.pdf&lt;/a&gt;&lt;br&gt;
&lt;a href="https://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/"&gt;https://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
