CSC/ECE 517 Fall 2011/ch3 3h rr: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 9: Line 9:
* Floating-point number
* Floating-point number
* Fixed-point number
* Fixed-point number
* Reference
* Reference ??


=== Boolean ===
=== Boolean ===

Revision as of 16:45, 6 October 2011

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?


Introduction

Programming languages, whether statically or dynamically typed, have support for certain in-built data types. These data types are the basic representation of information in programs and have certain fixed attributes for a specific language. 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. The primitive types commonly included in most programming languages are:

  • Boolean
  • Character
  • Integer
  • Floating-point number
  • Fixed-point number
  • Reference ??

Boolean

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.

Character

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.

Integer

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.

Floating-Point Number

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.


Fixed-Point number

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.

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.

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.