<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rpgodbol</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rpgodbol"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Rpgodbol"/>
	<updated>2026-08-09T04:49:47Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56492</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56492"/>
		<updated>2011-12-02T15:11:35Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic. &lt;br /&gt;
* Not much overhead with maintaining the money class objects.&lt;br /&gt;
* Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from [http://www.di-mare.com/adolfo/p/money.htm] is shown here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* The long datatype in the Zortech implementation cannot hold more than 10 decimal digits. &lt;br /&gt;
* This money class implements most arithmethic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = jsql&amp;gt; http://www.firstsql.com/javaobjects.shtml &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56491</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56491"/>
		<updated>2011-12-02T15:00:43Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = jsql&amp;gt; http://www.firstsql.com/javaobjects.shtml &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56490</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56490"/>
		<updated>2011-12-02T14:59:46Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = jsql&amp;gt; http://www.firstsql.com/javaobjects.shtml &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56489</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56489"/>
		<updated>2011-12-02T14:59:02Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = jsql&amp;gt; http://www.firstsql.com/javaobjects.shtml &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56488</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56488"/>
		<updated>2011-12-02T14:58:42Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = jsql&amp;gt; http://www.firstsql.com/javaobjects.shtml &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56487</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56487"/>
		<updated>2011-12-02T14:58:06Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values &amp;lt;ref name = jsql/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56486</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56486"/>
		<updated>2011-12-02T14:56:33Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
* The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56485</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56485"/>
		<updated>2011-12-02T14:54:57Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems [http://www.firstsql.com/javaobjects.shtml]. Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56484</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56484"/>
		<updated>2011-12-02T08:38:08Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. While the fundamentals are the same, there are a few subtle differences as well. There are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. &lt;br /&gt;
Eg. floor(double*21.32).&lt;br /&gt;
With this neat trick, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
&lt;br /&gt;
The one cool feature is that the Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56483</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56483"/>
		<updated>2011-12-02T07:44:09Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56482</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56482"/>
		<updated>2011-12-02T07:43:32Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the &amp;lt;ref name = Currency /&amp;gt;[http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56481</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56481"/>
		<updated>2011-12-02T07:42:13Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
 http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56480</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56480"/>
		<updated>2011-12-02T07:41:50Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = Currency&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56479</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56479"/>
		<updated>2011-12-02T07:39:54Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
* Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56478</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56478"/>
		<updated>2011-12-02T07:37:51Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
* The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
* An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56477</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56477"/>
		<updated>2011-12-02T06:43:53Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56476</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56476"/>
		<updated>2011-12-02T06:41:26Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* float&lt;br /&gt;
* double&lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56475</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56475"/>
		<updated>2011-12-02T06:40:37Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: &lt;br /&gt;
* int&lt;br /&gt;
* long&lt;br /&gt;
* BigDecimal&lt;br /&gt;
* Money Class (custom wrapper around BigDecimal)&lt;br /&gt;
Out of these, BigDecimal is the fundamental building block and custom Money Class implementations simply provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56474</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56474"/>
		<updated>2011-12-02T06:26:44Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal. Out of these, BigDecimal is the recommended method.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The int and long primitive types are very simple to use while representing cents/pennies.&lt;br /&gt;
* The BigDecimal representation has built-in rounding modes. &lt;br /&gt;
* BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
* Primitive types double and float carry small rounding differences.&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56472</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56472"/>
		<updated>2011-12-02T06:02:05Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal. Out of these, BigDecimal is the recommended method.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56471</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56471"/>
		<updated>2011-12-02T05:59:34Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56470</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56470"/>
		<updated>2011-12-02T05:51:20Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56469</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56469"/>
		<updated>2011-12-02T05:49:34Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56468</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56468"/>
		<updated>2011-12-02T05:49:13Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56467</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56467"/>
		<updated>2011-12-02T05:44:41Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  &lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56466</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56466"/>
		<updated>2011-12-02T05:43:49Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world.&lt;br /&gt;
One of the simplest ways to represent Money in Java programs is to use one of the following primitive types: int, long or BigDecimal.&lt;br /&gt;
&lt;br /&gt;
Here is an example snippet from [http://www.javapractices.com/topic/TopicAction.do?Id=13] that shows the use of BigDecimal to &lt;br /&gt;
&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  &lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56465</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56465"/>
		<updated>2011-12-02T05:32:46Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world.&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56464</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56464"/>
		<updated>2011-12-02T05:30:05Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself. Some of the most common operations for money types are implemented in the [http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html Currency] class, which encapsulates, among other things, standard identifiers for currencies around the world.&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56460</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56460"/>
		<updated>2011-12-02T04:37:17Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself.&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56459</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56459"/>
		<updated>2011-12-02T04:36:27Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* python-money (Python) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;br /&gt;
&lt;br /&gt;
== Money Gem&amp;lt;ref name = moneygemwebsite /&amp;gt; (Ruby) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The Ruby Money gem borrows quite a bit from the Skrien&amp;lt;ref name = skrien /&amp;gt; text methodologies while using the dynamic properties of Ruby to allow for more flexibility.   However, it should be noted that this implementation does not support an object containing mixed currencies as mentioned in the Skrien text and some behavior implemented goes against it's principles.&lt;br /&gt;
&lt;br /&gt;
The Money gem consists of a main '''Money''' class which houses several sub-classes and modules which allow for everything to be done from the main Money class directly without the users knowledge.  This implementation also mostly sticks to the idea that a Money object is immutable and thus any conversions and operations result in a new Money object.  The only exception to this is that the currency identifier for a Money object can be changed once it has been created.&lt;br /&gt;
&lt;br /&gt;
Internally, a Money object stores money as an integer in the form of '''cents'''.  It should be noted that while the variable is called cents, it can apply to all currencies which have a subunit of denomination.  Similar to the way the Skrien text uses the Currency and CurrencyConverter classes, the Money class uses internal classes for currency information and currency conversion as well, called '''Currency''' and '''Bank''' respectively.  The '''Money''' object stores a '''bank''' and '''currency''' as well as default bank and currency types at the class level.  &lt;br /&gt;
&lt;br /&gt;
The '''Currency''' class contains information such as the ISO4217&amp;lt;ref name = iso4217 /&amp;gt; three letter and number codes for common currencies as well as number of subunits in a denomination (ie, cents in a dollar) and delimiters for units.  Through the use of the currency class, Money objects can be initialized with a Currency value and have specific output formats based on currency.  The currency class also internally extends the '''CurrencyLoader''' class which allows for specifying different locations for loading currency information to allow for flexible currency management.  Below is an example of the USD currency information loaded by a default JSON configuration file:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  &amp;quot;usd&amp;quot;: {&lt;br /&gt;
    &amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
    &amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
    &amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
    &amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
    &amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
    &amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
    &amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
    &amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
    &amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
    &amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Bank class is also an internal class to the Money class but it resides at the class level.  This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency.  The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.  Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
The Money class also has multiple modules internally that expand the operations that can be performed on Money objects, including converting to and from numbers, strings and symbols.  Through the use of Ruby's dynamic nature, '''to_money()''' methods are added to the Numeric, String and Symbol types.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the official website &amp;lt;ref name = moneygemwebsite /&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
&lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents     #=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)    #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
&lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                     == Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                     == Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* The internal '''CurrencyLoader''' class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
* Due to the '''Bank''' implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
* The fact that the '''Currency''' and '''Bank''' classes are internal to the '''Money''' class, the user doesn't have to know about their implementation&lt;br /&gt;
* Ruby's dynamic nature allows for classes to be internal to '''Money''', yet defined in separate files from the '''Money''' class, helping to keep the code modular from a developers point of view &lt;br /&gt;
* The addition of '''to_money()''' methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
* Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a '''Money''' object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Doesn't support multiple currency types in a single object&lt;br /&gt;
* A '''Money''' object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
* While the addition of '''to_money()''' methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
* Since the '''Bank''' object is a singleton contained within the '''Money''' class, any modifications to currency exchange rates require locking of the '''Bank''' object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== python-money&amp;lt;ref name = pymoneywebsite /&amp;gt; (Python) ==&lt;br /&gt;
=== Overview ===&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python.  As such, it does not have as much functionality nor the complexities that many other implementations have.  &lt;br /&gt;
&lt;br /&gt;
The python-money module consists of two classes, '''Money''' and '''Currency'''.  Much like other implementations, the '''Money''' object contains an amount and currency internally.  The amount is specified as type Decimal&amp;lt;ref name = pydecimal /&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats.  When a '''Money''' object is created, either an identifier or '''Currency''' object is passed.  Below is an example of the '''Currency''' data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217&amp;lt;ref name = iso4217 /&amp;gt; standard three character and number identifiers.  This is what allows specifying currency by identifier rather than currency object.   Unfortunately this limits the currency types to those statically defined in the source code.  Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the '''Currency''' object.  This limits the way that '''Money''' objects can be converted.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
* Much like an integer value in Ruby, a Decimal&amp;lt;ref name = pydecimal /&amp;gt; has a theoretically infinite precision and maximum value.&lt;br /&gt;
* As long as the user does not access the internal structure of the '''Money''' object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
* Since the currency conversion data is set within the '''Currency''' object itself &lt;br /&gt;
** There is no locking present for the '''Currency''' object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
** The current implementation can only convert to and from the default currency type&lt;br /&gt;
* Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to '''Money''' objects, type checking has to be done on arguments to determine how to handle them.  This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrien /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
While Skrien introduces the Java Money class and then refines it through various stages to break it up into SimpleMoney and MixedMoney derived classes, there are various other implementations possible in Java itself.&lt;br /&gt;
&lt;br /&gt;
=== Overview ===&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrien&amp;gt; Object Oriented Design Using Java / Dale Skrien -- 1st Ed. &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = moneygemwebsite&amp;gt; http://money.rubyforge.org/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pymoneywebsite&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pydecimal&amp;gt; http://docs.python.org/library/decimal.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = iso4217&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56410</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56410"/>
		<updated>2011-12-01T20:44:22Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
7a. Representing money.  Skrien Chapter 6 gives an example of a class that can be used to represent money.  But how is it done in real programs?  Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56409</id>
		<title>CSC/ECE 517 Fall 2011/ch7 7a kr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch7_7a_kr&amp;diff=56409"/>
		<updated>2011-12-01T20:43:05Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2011/ch7 7a kr&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
A common task in real world application programs is conducting operations on and manipulating money values. The base data type used to represent money can be one of several possible implementations. Object oriented languages have the ability to represent this information using classes. Primitive data types such as integers, floating point numbers can also be used to represent money. There are certain inherent advantages and disadvantages with each of these methods of representation. This wiki article explores these different implementations and analyzes their relative merits and demerits. We also focus on the various operations that can be performed with money values and analyze how different languages and schemes help while working with money values.&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51917</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51917"/>
		<updated>2011-10-08T03:36:51Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a brief analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Ease of testing for comparison: With primitive types, the equality testing operators such as == can be used. These essentially compare the values stored in the primitive types. Regular objects also offer the eql? method for testing equality. However, the following are not equivalent:&lt;br /&gt;
  a=10&lt;br /&gt;
  =&amp;gt; 10&lt;br /&gt;
  a==10&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a==10.0&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a.eql?(10.0)&lt;br /&gt;
  =&amp;gt; false&lt;br /&gt;
The reason the .eql? fails is that this operator tests for value and type being the same. 10 is type Fixnum and 10.0 is type Float. &lt;br /&gt;
The eql? can be overridden by == for primitive objects if you wish to compare only the values, but that can have a negative impact on performance [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html].&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability: The primitive data types in languages such as Java cannot be inherited to create further subtypes.&lt;br /&gt;
* Unexpected results due to method overriding: There are certain examples such as [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html], which show that overriding inbuilt methods such as == and eql? can lead to unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51916</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51916"/>
		<updated>2011-10-08T01:51:34Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Merit Analysis of Primitive Types */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a brief analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Simplicity in testing types for comparison: With primitive types, the equality testing operators such as == can be used. These essentially compare the values stored in the primitive types. Regular objects also offer the eql? method for testing equality. However, the following are not equivalent:&lt;br /&gt;
  a=10&lt;br /&gt;
  =&amp;gt; 10&lt;br /&gt;
  a==10&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a==10.0&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a.eql?(10.0)&lt;br /&gt;
  =&amp;gt; false&lt;br /&gt;
The reason the .eql? fails is that this operator tests for value and type being the same. 10 is type Fixnum and 10.0 is type Float. &lt;br /&gt;
The eql? can be overridden by == for primitive objects if you wish to compare only the values, but that can have a negative impact on performance [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html].&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability: The primitive data types in languages such as Java cannot be inherited to create further subtypes.&lt;br /&gt;
* Unexpected results due to method overriding: There are certain examples such as [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html], which show that overriding inbuilt methods such as == and eql? can lead to unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51915</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51915"/>
		<updated>2011-10-08T01:50:44Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Simplicity in testing types for comparison: With primitive types, the equality testing operators such as == can be used. These essentially compare the values stored in the primitive types. Regular objects also offer the eql? method for testing equality. However, the following are not equivalent:&lt;br /&gt;
  a=10&lt;br /&gt;
  =&amp;gt; 10&lt;br /&gt;
  a==10&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a==10.0&lt;br /&gt;
  =&amp;gt; true&lt;br /&gt;
  a.eql?(10.0)&lt;br /&gt;
  =&amp;gt; false&lt;br /&gt;
The reason the .eql? fails is that this operator tests for value and type being the same. 10 is type Fixnum and 10.0 is type Float. &lt;br /&gt;
The eql? can be overridden by == for primitive objects if you wish to compare only the values, but that can have a negative impact on performance [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html].&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability: The primitive data types in languages such as Java cannot be inherited to create further subtypes.&lt;br /&gt;
* Unexpected results due to method overriding: There are certain examples such as [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html], which show that overriding inbuilt methods such as == and eql? can lead to unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51914</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51914"/>
		<updated>2011-10-07T06:19:07Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability: The primitive data types in languages such as Java cannot be inherited to create further subtypes.&lt;br /&gt;
* Unexpected results due to method overriding: There are certain examples such as [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html], which show that overriding inbuilt methods such as == and eql? can lead to unexpected results.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51913</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51913"/>
		<updated>2011-10-07T06:17:12Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
* Unexpected results due to method overriding&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51912</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51912"/>
		<updated>2011-10-07T06:13:24Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. &lt;br /&gt;
  So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51911</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51911"/>
		<updated>2011-10-07T06:11:42Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Primitive Objects in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| No physical limit, but can be decided by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51910</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51910"/>
		<updated>2011-10-07T06:10:54Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Primitive Objects in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! Description&lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
| true&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
| false&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
| See Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
| Machine architecture dependent. 2^30-1 to -2^30 on 32-bit machines.&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
| Machine architecture dependent. Values above Fixnum range.&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
| Value after decimal point can be formatted&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
| Limit imposed by machine architecture&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51909</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51909"/>
		<updated>2011-10-07T05:51:56Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Primitive Objects in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
| Description&lt;br /&gt;
| Range&lt;br /&gt;
|-&lt;br /&gt;
| TrueClass&lt;br /&gt;
| Singleton instance &amp;quot;true&amp;quot; allowed&lt;br /&gt;
|-&lt;br /&gt;
| FalseClass&lt;br /&gt;
| Singleton instance &amp;quot;false&amp;quot; allowed&lt;br /&gt;
|-&lt;br /&gt;
| Integer [http://www.ruby-doc.org/core/Integer.html]&lt;br /&gt;
| Abstract class that forms the basis for Fixnum and Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum [http://www.ruby-doc.org/core-1.8.7/Fixnum.html]&lt;br /&gt;
| Integer representations that fit in native machine word&lt;br /&gt;
|-&lt;br /&gt;
| Bignum [http://www.ruby-doc.org/core/Bignum.html]&lt;br /&gt;
| Integer representations that do not fit in Fixnum width&lt;br /&gt;
|-&lt;br /&gt;
| Float [http://www.ruby-doc.org/core/Float.html]&lt;br /&gt;
| Real numbers using double precision representation&lt;br /&gt;
|-&lt;br /&gt;
| String [http://corelib.rubyonrails.org/classes/String.html]&lt;br /&gt;
| Contains sequence of characters&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
One interesting observation from the above table is that Ruby does not have a Boolean class; instead it has a separate TrueClass and FalseClass [http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby].&lt;br /&gt;
&lt;br /&gt;
  puts true.class &lt;br /&gt;
  =&amp;gt; TrueClass&lt;br /&gt;
  puts false.class&lt;br /&gt;
  =&amp;gt; FalseClass&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51908</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51908"/>
		<updated>2011-10-07T05:30:14Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
|-&lt;br /&gt;
| Boolean&lt;br /&gt;
|-&lt;br /&gt;
| Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum&lt;br /&gt;
|-&lt;br /&gt;
| Float&lt;br /&gt;
|-&lt;br /&gt;
| Integer&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;br /&gt;
# http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51907</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51907"/>
		<updated>2011-10-07T05:30:03Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
|-&lt;br /&gt;
| Boolean&lt;br /&gt;
|-&lt;br /&gt;
| Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum&lt;br /&gt;
|-&lt;br /&gt;
| Float&lt;br /&gt;
|-&lt;br /&gt;
| Integer&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
* Comparison operation&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51906</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51906"/>
		<updated>2011-10-07T03:36:46Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Primitive Objects in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
|-&lt;br /&gt;
| Boolean&lt;br /&gt;
|-&lt;br /&gt;
| Bignum&lt;br /&gt;
|-&lt;br /&gt;
| Fixnum&lt;br /&gt;
|-&lt;br /&gt;
| Float&lt;br /&gt;
|-&lt;br /&gt;
| Integer&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51905</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51905"/>
		<updated>2011-10-07T03:33:11Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51904</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51904"/>
		<updated>2011-10-07T03:32:24Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Primitive Objects in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types or objects in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51903</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51903"/>
		<updated>2011-10-07T03:31:23Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
* Example [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html]&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types or objects in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
* Lack of inheritance capability&lt;br /&gt;
*&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51902</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51902"/>
		<updated>2011-10-07T03:25:00Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
* Example [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html]&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types or objects in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
  Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
  Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51901</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51901"/>
		<updated>2011-10-07T03:24:04Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
* Example [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html]&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types or objects in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
Eg. [http://corelib.rubyonrails.org/classes/String.html String] primitive object provides convenience methods such as upcase to convert the entire string to upper case, or capitalize, which converts only the first character to upper case.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51898</id>
		<title>CSC/ECE 517 Fall 2011/ch3 3h rr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch3_3h_rr&amp;diff=51898"/>
		<updated>2011-10-07T03:13:21Z</updated>

		<summary type="html">&lt;p&gt;Rpgodbol: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''3h. Primitive objects.  At the beginning of Lecture 11, we discovered that Fixnums and Bignums are handled differently behind the scenes in Ruby.  Other languages, like Java, have made similar distinctions.  By contrast, languages such as C# and Eiffel try to hide these implementation differences from users.  Answer two questions: (1) How have different o-o languages implemented primitive objects?  E.g., how are they represented in memory, how are they tested for, do comparisons do anything different than for class objects, etc.  (2) What are the advantages and disadvantages of treating primitives differently from class objects in source code?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types, known as primitive types, are the basic representation of information in programs and have certain fixed attributes for a specific language[http://en.wikipedia.org/wiki/Primitive_data_type]. Statically typed languages such as C++, Java, Perl etc. support primitive data types, whereas with dynamically typed languages such as Ruby, Smalltalk, Lisp etc. they are actually in the form of primitive objects.  These primitive types are used to store the basic types of information that a computer can store and manipulate, and can also be used as building blocks for creating more complex data types. &lt;br /&gt;
This article explains the way different primitive types are implemented in certain object oriented languages. An analysis of the benefits and drawbacks of such types and the methods used to operate on them is also presented. &lt;br /&gt;
&lt;br /&gt;
== Primitive Types ==&lt;br /&gt;
The primitive types commonly included in most programming languages are:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Character&lt;br /&gt;
* Integer&lt;br /&gt;
* Floating-point number&lt;br /&gt;
* Fixed-point number&lt;br /&gt;
* Reference&lt;br /&gt;
&lt;br /&gt;
=== Boolean ===&lt;br /&gt;
A Boolean is a primitive data type used to store one of two logical types: true or false.  Boolean data types are most commonly used as input paramters to a conditional statement (such as an ‘if’ statement), or as the output of a comparison between two comparable data types.  Booleans can be implemented in languages as either a discrete logical type, or implicitly as a numerical type.  In many languages, booleans can be implicitly converted to and from integer types.  &lt;br /&gt;
&lt;br /&gt;
=== Character ===&lt;br /&gt;
A character is a data type that represents an element of a written language, such as a letter, number, or symbol.  A character can also represent a control character, such as a carriage return or newline, which does not have a written meaning but controls how other characters are stored or displayed.   Characters are commonly stored as integers, and encoded using a character map.  &lt;br /&gt;
&lt;br /&gt;
=== Integer ===&lt;br /&gt;
An integer is a data type that represents one element of a finite subset of mathematical integers.  Integer, or Integral, data types can be either unsigned (able to store only positive whole numbers) or signed (able to store either positive or negative whole numbers).  The range of values that can be represented by an integer depends on the number of bits used to store the integer, whether or not it is a signed integer, and the encoding scheme (if it is signed).  Typically, an integer has a minimum and maximum value, and can store any integer in the range between those values.  The minimum value for unsigned integers is typically 0, and the maximum value is typically determined by the amount of memory used to store the integer.  For example, a un unsigned 8-bit number can store 2^8 (or 256) possible integral values; and would typically store any value from 0 to 255.  More generally, an n-bit unsigned integer can store from 0 to (2^n)-1.  For signed integers, modern computers use the Two’s Complement encoding scheme.  This allows for a range of −2^(n−1) through 2^(n−1)−1.  For example, an 8-bit signed integer could store any whole number in the range from -128 through +127.&lt;br /&gt;
&lt;br /&gt;
=== Floating-Point Number ===&lt;br /&gt;
A floating point number is a data type used to represent real numbers in a large range with varying degrees of precision.  In this representation, numbers are represented with a variable number of significant digits, and a variable number of exponential digits.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Fixed-Point number ===&lt;br /&gt;
A fixed-point number is a data type used to represent real numbers.  Fixed-point numbers are called fixed-point because they have a set number of digits before and after a decimal mark.  In this regard, fixed-point numbers are represented as an integer, but are scaled by a predetermined factor.  &lt;br /&gt;
&lt;br /&gt;
Fixed-point numbers are commonly used in microprocessors that do not have a floating-point unit, or in systems in which computational efficiency is critical.  Fixed-point numbers can be treated as integers by an arithmetic logic unit (ALU) and scaled after a result is obtained, which can significantly lower the amount of time needed for a processor to obtain the result for some algorithms. &lt;br /&gt;
&lt;br /&gt;
Implementing algorithms using fixed-point arithmetic requires great care, because of the potential for information loss.  Fixed-point arithmetic operations -- multiplication in particular, has the potential to cause overflow.  Algorithms must be written with care to ensure that each term of an equation has a similar range and that the result will not cause an overflow.&lt;br /&gt;
&lt;br /&gt;
=== Reference ===&lt;br /&gt;
&lt;br /&gt;
A Reference is a data type that enables a program to access another item in memory.  A reference differs from other primitive data types in that it does not store data itself; instead it stores a value referring to another data object.  References are commonly used to refer to objects of large non-primitive data types.  References commonly store the physical memory address of the data that they are referring to.  Accessing the data referred to by a Reference is called dereferencing.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C++ ==&lt;br /&gt;
&lt;br /&gt;
C++ is a statically-typed object oriented language.  C++ is based on the C programming language, which is procedural, and adds support for object-oriented code.  &lt;br /&gt;
&lt;br /&gt;
These data types are defined in C++: [http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php] &lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
| void &lt;br /&gt;
| N/A &lt;br /&gt;
| the void data type is used to explicitly identify that a data has no type &lt;br /&gt;
| N/A&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits&lt;br /&gt;
| simple numerical type&lt;br /&gt;
| See [[http://www.jk-technology.com/c/inttypes.html]]&lt;br /&gt;
|-&lt;br /&gt;
| float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 8 bits &lt;br /&gt;
| a char is a single 8-bit character encoded using ASCII &lt;br /&gt;
| Ascii character 0x00 through ascii character 0xFF&lt;br /&gt;
|}&lt;br /&gt;
C++ supports Pointers for all of the types listed in the table above, as well as more complex data types (such as structs).  A Pointer in C++ is a data type that stores the physical address of some other data.  Pointers are created in C++ by using the * operator.  For example, a *Double[] is a pointer to an array of double-precision floating point numbers.  C++ Also supports function pointers -- pointers that reference the beginning address of a function in memory.  They are commonly used to implement callback functions [http://newty.de/fpt/intro.html#what]&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in Java == &lt;br /&gt;
&lt;br /&gt;
Java is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  These data types are defined in Java: [http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html]&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;|Name&lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte&lt;br /&gt;
|8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
|-128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
|16 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647 &lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| See [[http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]]&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Java also defines a String class, which is used to create objects of many chars.  The String class provides functionality commonly implemented using arrays of chars in other languages, such as C. &lt;br /&gt;
&lt;br /&gt;
Java also defines the 'unsigned' keyword, which can be used to as a modifier to any of the integral types listed in the table above.  If the 'unsigned' keyword is used, the integral type will be unsigned instead of signed, and its range will change correspondingly. &lt;br /&gt;
&lt;br /&gt;
Java is capable of using any two objects of the same primitive data type for comparison.  Java defines a class for each data type, which have the same name but a capitalized first letter (e.g. Float instead of float).  These classes, called wrapper classes provide a series of methods that can manipulate their associated primitive data type, as well as convert to and from other data types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Data Types in C# == &lt;br /&gt;
&lt;br /&gt;
C# is a statically-typed object oriented programming language.  Primitive types are defined in the language, and conversion between them must be explicitly performed.  Primitive data types are created using a keyword, which is also the name of the data type.  C# has all of the data types that are available in Java, as well as some additional ones.  &lt;br /&gt;
&lt;br /&gt;
Similar to Java, C# defines a String class which is used to create objects of many chars.  These data types are defined in C#: [http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx]&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
! align=&amp;quot;left&amp;quot;| Name &lt;br /&gt;
! .NET Class &lt;br /&gt;
! Size &lt;br /&gt;
! Description &lt;br /&gt;
! Range&lt;br /&gt;
|-&lt;br /&gt;
|byte &lt;br /&gt;
| Byte &lt;br /&gt;
| 8 bits &lt;br /&gt;
|signed two's complement integer &lt;br /&gt;
| -128 to 127 &lt;br /&gt;
|-&lt;br /&gt;
| sbyte &lt;br /&gt;
| SByte &lt;br /&gt;
| 8 bits&lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -0 to 255&lt;br /&gt;
|-&lt;br /&gt;
| short &lt;br /&gt;
| Int16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -32,768 to 32,767&lt;br /&gt;
|-&lt;br /&gt;
| ushort &lt;br /&gt;
| UInt16 &lt;br /&gt;
| 16 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 65,535&lt;br /&gt;
|-&lt;br /&gt;
| int &lt;br /&gt;
| Int32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -2,147,483,648 to 2,147,483,647&lt;br /&gt;
|-&lt;br /&gt;
| uint &lt;br /&gt;
| UInt32 &lt;br /&gt;
| 32 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 4,294,967,295&lt;br /&gt;
|-&lt;br /&gt;
| long &lt;br /&gt;
| Int64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| signed two's complement integer &lt;br /&gt;
| -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807&lt;br /&gt;
|-&lt;br /&gt;
| ulong &lt;br /&gt;
| UInt64 &lt;br /&gt;
| 64 bits &lt;br /&gt;
| unsigned integer &lt;br /&gt;
| 0 to 18,446,744,073,709,551,615&lt;br /&gt;
|- &lt;br /&gt;
|float &lt;br /&gt;
| Float &lt;br /&gt;
| 32 bits &lt;br /&gt;
| single-precision IEEE 754 floating point &lt;br /&gt;
| -3.402823e38 to 3.02823e38&lt;br /&gt;
|-&lt;br /&gt;
| double &lt;br /&gt;
| Double &lt;br /&gt;
| 64 bits &lt;br /&gt;
| double-precision IEEE 754 floating point&lt;br /&gt;
| -1.79769313486232e308 to 1.79769313486232e308&lt;br /&gt;
|- &lt;br /&gt;
| boolean &lt;br /&gt;
| Boolean &lt;br /&gt;
| 1 bit &lt;br /&gt;
| boolean &lt;br /&gt;
| false, true&lt;br /&gt;
|-&lt;br /&gt;
| char &lt;br /&gt;
| Char &lt;br /&gt;
| 16 bits &lt;br /&gt;
| a char is a single 16-bit character encoded using Unicode &lt;br /&gt;
| Unicode character \u0000 through unicode character \uffff&lt;br /&gt;
|-&lt;br /&gt;
| object &lt;br /&gt;
| Object &lt;br /&gt;
| N/A &lt;br /&gt;
| Object is the base type of all other types&lt;br /&gt;
|-&lt;br /&gt;
| string &lt;br /&gt;
| String &lt;br /&gt;
| N/A &lt;br /&gt;
| String is the base type for a sequence of chars&lt;br /&gt;
|-&lt;br /&gt;
| decimal &lt;br /&gt;
| Decimal &lt;br /&gt;
| 128 &lt;br /&gt;
| Decimal is an integral type that can represent a decimal number with 29 significant digits ±1.0 × 10e−28 to ±7.9 × 10e28&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Like Java, each primitive data type in C# also has a class associated with it that.  These classes serve a similar purpose to their associated ones in Java.  They are used for comparison of objects, as well as conversion between other similar types.&lt;br /&gt;
&lt;br /&gt;
== Primitive Objects in Ruby ==&lt;br /&gt;
Ruby is a pure object oriented language as compared to languages such as Java or C#, which use a more hybrid approach. In Ruby, all data types are represented as Objects. There are some [http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html inbuilt classes] that are provided to users in Ruby. However, only some of them are a basic building block for forming other types. This subset shown below gives us a list of primitive objects that can be used for data representation and manipulation:&lt;br /&gt;
* Boolean&lt;br /&gt;
* Bignum&lt;br /&gt;
* Fixnum&lt;br /&gt;
* Float&lt;br /&gt;
* Integer&lt;br /&gt;
* String&lt;br /&gt;
&lt;br /&gt;
Although types such as Array, Hash are also in-built types, they can be further composed of elements that are internally represented in one of the primitive types. Hence, they will not be treated by us as primitive objects, in the traditional definition of the term.&lt;br /&gt;
Each of the primitive objects listed above also provide certain convenience methods that are applicable for the underlying type.&lt;br /&gt;
For example, the Fixnum, Bignum and Float types provide support for arithmetic operations such as addition (+), subtraction(--), multiplication(*) and so on.&lt;br /&gt;
As with all other classes in Ruby, users can add functionality to existing primitive objects by reopening classes. The amount of memory required to implement the primitive objects in Ruby is machine dependent in some cases.&lt;br /&gt;
&lt;br /&gt;
* Example [http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html]&lt;br /&gt;
&lt;br /&gt;
== Merit Analysis of Primitive Types ==&lt;br /&gt;
This section deals with a holistic analysis of the relative merits and demerits of primitive data types. While we focus on Java or Ruby for this purpose, most of these points are applicable across all object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=== Advantages ===&lt;br /&gt;
Primitive types or objects in object oriented languages have certain advantages over their class object counterparts. &lt;br /&gt;
* Simplicity: Primitive types/objects provide users a simple mechanism of manipulating data without relying on additional objects to achieve the same functionality. Operations on primitive types are more intuitive.&lt;br /&gt;
* Efficiency: This statement is applicable if the underlying primitive object definition is not modified (a feature that languages such as Ruby provide to users). As the representation in memory is designed to be make most efficient use of the underlying datatype, use of primitives can provide a benefit to the user, over the use of class objects to store the same data. &lt;br /&gt;
Eg. Java provides wrappers [http://www.glenmccl.com/tip_016.htm] for certain primitive types. There is a certain performance and space cost associated with these. So, to maximize efficiency, direct use of the primitive types would provide the most benefit.&lt;br /&gt;
* Ability to use inbuilt methods: Depending on the primitive type, languages such as Ruby provide methods that can be used specifically to probe or manipulate objects. &lt;br /&gt;
Eg.&lt;br /&gt;
&lt;br /&gt;
=== Disadvantages ===&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Object oriented languages have varying levels of support for primitive data types and objects. Whether they are beneficial or not depends on the application to a great deal. If handled correctly, they can make object oriented programs more efficient. However, the user needs to be aware of the underlying representation of these types to handle any unexpected results.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
# http://en.wikipedia.org/wiki/Primitive_data_type&lt;br /&gt;
# http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php&lt;br /&gt;
# http://www.jk-technology.com/c/inttypes.html&lt;br /&gt;
# http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html&lt;br /&gt;
# http://newty.de/fpt/intro.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/ms228360%28v=vs.80%29.aspx&lt;br /&gt;
# http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html&lt;br /&gt;
# http://blog.vishnuiyengar.com/2009/09/primitive-obsession-in-ruby-aka-not.html&lt;br /&gt;
# http://www.glenmccl.com/tip_016.htm&lt;/div&gt;</summary>
		<author><name>Rpgodbol</name></author>
	</entry>
</feed>