<?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: Mohamed Ayman</title>
    <description>The latest articles on DEV Community by Mohamed Ayman (@devmayman).</description>
    <link>https://dev.to/devmayman</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%2F1376303%2F23933c19-bbda-44e3-bd2e-cfb6f654824e.jpeg</url>
      <title>DEV Community: Mohamed Ayman</title>
      <link>https://dev.to/devmayman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmayman"/>
    <language>en</language>
    <item>
      <title>Floating Point Representation (IEEE 754 ISSUE)</title>
      <dc:creator>Mohamed Ayman</dc:creator>
      <pubDate>Thu, 04 Jul 2024 18:29:54 +0000</pubDate>
      <link>https://dev.to/devmayman/floating-point-representation-ieee-754-issue-9f3</link>
      <guid>https://dev.to/devmayman/floating-point-representation-ieee-754-issue-9f3</guid>
      <description>&lt;h2&gt;
  
  
  How can computers understanding Floating Point Numbers?
&lt;/h2&gt;

&lt;p&gt;To understand how computers interpret decimal numbers like 18.50, we need to think like a computer. For us, it's straightforward—we see 18.50, but for a computer, it's a bit more complex. Computers use binary, where 5 is represented as 101. However, 101 represents 5, not 0.5. We might think to store 0.5 as 0.101, but computers don't interpret the decimal point like we do. It's merely a visual aid for us, indicating the separation between whole numbers and fractions. Understanding how computers manage and retrieve this data is key to comprehending their processing of floating-point numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many bits for floating number?
&lt;/h2&gt;

&lt;p&gt;This is a second problem if i represent floating number in computer, How many digits should i take to store mantissa and integer number. In integer data type we take 4 byte but now may number in float data type has two part integer part and mantissa part. Some one says in my application i want the integer part take more digits than mantissa so like we know float data type takes 32 bits (4 byte) some one says i will take 20 bits for integer and 11 for mantissa and the rest bit for the sign -positive or negative-&lt;br&gt;
while another one will say i will take first 24 bits for integer and 7 for mantissa and the rest bit for sign. It is so clear that there is different implementations, in the programming when scientists ans manufacturer saw that the quickly go to standard we in need now to make standard to unify the implementation so coder and developer do not find it so hard to learn different implementations for data type.&lt;br&gt;
The question now how we can make standard ?&lt;br&gt;
we need engineers that related to electronics and electric as we know binary system is just voltage, we transistor has voltage, its value will be 1 and when has not the value will be 0. Now we will make s from Electrical and electronic engineers called IEEE -Institute of Electrical and Electronic Engineer- and when we need make institute it will be the name of the institute with number indicates to this standard. If you want read about floating number representation it will be at IEEE 754 standard.&lt;/p&gt;

&lt;p&gt;Do not be confused, i will summarize the two problems. We have problems, first how computer will understand float number (if you said point character, we said that is human visual representation and human knows what is this so the can make their calculation in their mind ) and second one is how many bits for mantissa part and integer part. We need standard so everyone will not make its implementation.&lt;/p&gt;

&lt;p&gt;Now i will try to explain some separated topics that we will need it in explaining IEEE 754.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Mantissa and Exponent
&lt;/h2&gt;

&lt;p&gt;This is the way how computer store and retrieve floating number&lt;br&gt;
if you have number 6000.11 it can also read as 0.600011 x 104&lt;br&gt;
Now computer can understand this representation&lt;br&gt;
and when computer retire it will be so easy, the representation became as computer login can understand, addition and multiplication process.&lt;/p&gt;

&lt;p&gt;The question is why this is bad behavior in floating number?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64cj3mnwem5l42lsl1ox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F64cj3mnwem5l42lsl1ox.png" alt="Image description" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let start step by step demonstrate this example:&lt;/p&gt;

&lt;p&gt;Convert 9 to binary &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5rrrwsjqjfszrsv6f4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5rrrwsjqjfszrsv6f4f.png" alt="Image description" width="249" height="291"&gt;&lt;/a&gt;&lt;br&gt;
Convert 0.1 to binary -The problem start from here- In this picture i will convert 0.1 and 0.5 to binary to make sure that you understand how we can convert mantissa to binary &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvzeworekt446taywp77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvzeworekt446taywp77.png" alt="Image description" width="504" height="354"&gt;&lt;/a&gt; Now the number is like 1001.0001100..... It is so clear that conversion from 0.1 to binary will go to infinity&lt;br&gt;
Standard says that in float data type mantissa will take 23 bits and exponent 8 bits and the last bit for sign&lt;br&gt;
put it in mantissa and exponent representation 1001.00011001100110011010 -&amp;gt; 1.001000110011001100110011 there are 24 bits after point character. the rule is if the mantissa more than 23 bits if the 24th bit is 1 so 1 will be added for mantissa but if is it is 0 we will take the previous 23 bits without adding any thing &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodu15bmrq79m1xdaev1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodu15bmrq79m1xdaev1i.png" alt="Image description" width="800" height="675"&gt;&lt;/a&gt;&lt;br&gt;
Convert 0.1 &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzb8897murc41mqq22lvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzb8897murc41mqq22lvs.png" alt="Image description" width="800" height="724"&gt;&lt;/a&gt;&lt;br&gt;
Now i use online converter from binary to decimal to convert this two number This is for 0.1 convertion from binary representation to decimal one &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96nlu74k8h0t69rolf4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96nlu74k8h0t69rolf4g.png" alt="Image description" width="521" height="842"&gt;&lt;/a&gt; and this for 9.1 &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbd2n4dwexrq4v1fsr3o7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbd2n4dwexrq4v1fsr3o7.png" alt="Image description" width="521" height="842"&gt;&lt;/a&gt;&lt;br&gt;
The result is &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffm6270to8vdjidf9qw69.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffm6270to8vdjidf9qw69.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This bad behavior in floating point representation was counted in java language by made another data type called big Decimal. It handle this behavior but it is slower than float. &lt;br&gt;
It is up to do you prefer big Decimal as you need more precision in you code but you will take slower performance than float or you do not need a high precision and will choose float data type.&lt;/p&gt;

</description>
      <category>ieee</category>
      <category>float</category>
      <category>binar</category>
      <category>numbers</category>
    </item>
    <item>
      <title>File Input and Output in Ruby</title>
      <dc:creator>Mohamed Ayman</dc:creator>
      <pubDate>Fri, 03 May 2024 18:48:46 +0000</pubDate>
      <link>https://dev.to/devmayman/file-input-and-output-in-ruby-1ajb</link>
      <guid>https://dev.to/devmayman/file-input-and-output-in-ruby-1ajb</guid>
      <description>&lt;p&gt;As we know that Ruby one of the language for backend technology, so it must have a module to deal with file system in your Operating system. &lt;/p&gt;

&lt;p&gt;Ruby I/O is a way to interact with your system. Data is sent in the form of bytes/characters. IO class is the basis for all input and output in Ruby. It may be duplexed, hence may use more than one native operating system stream.&lt;/p&gt;

&lt;p&gt;IO has a subclass as File class which allows reading and writing files in Ruby. The two classes are closely associated. IO object represent readable/writable interactions to keyboards and screens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common modes in I/O port
&lt;/h2&gt;

&lt;p&gt;As it is known in most of  other languages that have file system module, they have modes when dealing with file.&lt;/p&gt;

&lt;p&gt;like if you want to append, delete, add, or read and this modes in ruby when  you dealing with files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"r": read-only mode is the default mode starts at beginning of file.&lt;/li&gt;
&lt;li&gt;"r+": read-write mode, starts at beginning of file.&lt;/li&gt;
&lt;li&gt;"w": write-only mode, either creates a new file or truncates an existing file for writing.&lt;/li&gt;
&lt;li&gt;"w+": read-write mode, either creates a new file or truncates an existing file for reading and writing.&lt;/li&gt;
&lt;li&gt;"a": write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.&lt;/li&gt;
&lt;li&gt;"a+": read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.- "r": read-only mode is the default mode starts at beginning of file.&lt;/li&gt;
&lt;li&gt;"r+": read-write mode, starts at beginning of file.&lt;/li&gt;
&lt;li&gt;"w": write-only mode, either creates a new file or truncates an existing file for writing.&lt;/li&gt;
&lt;li&gt;"w+": read-write mode, either creates a new file or truncates an existing file for reading and writing.&lt;/li&gt;
&lt;li&gt;"a": write-only mode, if file exists it will append the file otherwise a new file will be created for writing only.&lt;/li&gt;
&lt;li&gt;"a+": read and write mode, if file exists it will append the file otherwise a new file will be created for writing and reading.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ruby opening a file
&lt;/h2&gt;

&lt;p&gt;A Ruby file can be created using different methods for reading, writing or both.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fileName.rb"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fileName.rb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"mode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The different between open and new methods that open can take block of code and determine a specific mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ruby reading a file
&lt;/h2&gt;

&lt;p&gt;There are three different methods to read a file.&lt;/p&gt;

&lt;p&gt;To return a single line, following syntax is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;gets&lt;/span&gt;   
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;   
&lt;span class="k"&gt;end&lt;/span&gt;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To return the whole file after the current position, following syntax is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To return file as an array of lines, following syntax is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readlines&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ruby writing a file
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/ruby   &lt;/span&gt;

&lt;span class="n"&gt;aFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"about.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"r+"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;aFile&lt;/span&gt;   
   &lt;span class="n"&gt;aFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;syswrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"New content is written in this file.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
&lt;span class="k"&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ruby renaming and deleting a file
&lt;/h2&gt;

&lt;p&gt;Ruby files are renamed using rename method and deleted using delete mehtod.&lt;/p&gt;

&lt;p&gt;To rename a file, following syntax is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"old.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"new.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To delete a file, following syntax is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"filename.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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