algorytm.org Algorithms For beginners Absolute value  
Home AlgorithmsData structuresAlgorithmics turorialPractiseDesign patternsIT Law SitemapPortal historyContributors ForumToolsWrite an articleSearch 

Absolute value
User Rating: / 41
PoorBest 
Written by Tomasz Lubiński   
Sunday, 13 September 2009 11:31

Calculation of the absolute value of the given number is the simplest example of using the algorithm with a decision. For a given number x the absolute value |x| is defined as:

  • x, if x ≥ 0,
  • -x, otherwise.



The operation of calculating the absolute value of x we can present by the following block diagram:

block diagram - absolute value



Example:
Assume that x is equal to 5. We check decision "if 5 ≥ 0?". Yes - so the result is equal to x, so it is 5.
Assume that x is equal to -3, now. We check decision "if -3 ≥ 0?". It is equal to No, so result is equal to -x, so it is - -3, two minuses give us +, so the final result is equal to 3.


.

Author Progam language Comment Download Rate
Tomasz Lubiński C# MS Visual Studio .net
Implementation in C#
/ 0
Tomasz Lubiński C/C++
Implementation in C/C++
/ 0
Tomasz Lubiński Delphi/Pascal
Implementation in Delphi/Pascal
/ 0
Tomasz Lubiński Java
Implementation in Java
/ 0
Hubert Pietrusiak Python Python 2.6 - UTF-8
Implementation in Python
/ 1
 
Add your implementation for this algorithm
  • Login first
File:
Progam language:
Comment:
  To be able to add your implementation, login first



Last Updated on Thursday, 27 May 2010 18:59
 

Add comment







Danation
Donate us


RSS Channels
Articles
Implementations
Comments
Forum


Bookmarks








Poll
Czy znalazłeś na stronach www.algorytm.org to czego szukałeś?
 

www.algorytm.org (c) 2000-2010