PLC : SESSION 4 (Data Type) (GSLC)

In this session, we have GSLC class so Ms.Yanfi only gave us some questions through the Binusmaya Forum and gave us about a week to answer it through the forum.

 

Here is the question:

  1. What are the advantages and disadvantages of decimal data types?
  2. How does a decimal value waste memory space?
  3. In what way is the static type checking better than dynamic type checking?
  4. What is coercion in programming languages?
  5. Explain how coercion rules can weaken the beneficial effect of strong typing?

 

And here are the answers I wrote on the forum:

  1. The advantages of decimal data types is being able to precisely store decimal values, at least those within a restricted range, which cannot be done with floating-point and accuracy.

The disadvantages of decimal data types are that that the range of value is                               restricted because no exponents are allowed, and their representation in                                   memory is mildly wasteful.

 

  1. Decimal types are stored very much like character strings, using binary codes for the decimal digits. These representations are called binary coded decimal (BCD). In some cases, they are stored one digit per byte, but in others, they are packed two digits per byte. Either way, they take more storage than binary representations. It takes at least four bits to code a decimal digit. Therefore, to store a six-digit coded decimal number requires 24 bits of memory. However, it takes only 20 bits to store the same number in binary. Decimal values store numeric information as digits encoded using the four bit binary equivalents: 0 (0000) to 9 (1001). That means a single byte can hold values between 0 and 99. But simply using the same byte to hold a binary value will yield values between 0 and 255 (or –128 and +127).

 

  1. The big benefit of static type checking is that it allows many type errors to be caught early in the development cycle. Static typing usually results in compiled code that executes more quickly because when the compiler knows the exact data types that are in use, it can produce optimized machine code (i.e. faster and/or using less memory). Static type checkers evaluate only the type information that can be determined at compile time, but are able to verify that the checked conditions hold for all possible executions of the program, which eliminates the need to repeat type checks every time the program is executed.

 

  1. Many programming languages support the conversion of a value into another of a different data type. This kind of type conversions can be implicitly or explicitly made. Implicit conversion, which is also called coercion, is automatically done. Explicit conversion, which is also called casting, is performed by code instructions. This code treats a variable of one data type as if it belongs to a different data type. The languages that support implicit conversion define the rules that will be automatically applied when primitive compatible values are involved. The C code below illustrates implicit and explicit coercion. In line 2 the int constant 3 is automatically converted to double before assignment (implicit coercion). An explicit coercion is performed by involving the destination type with parenthesis, which is done in line 3.

A. double x, y;

B. 2 x = 3; // implicitly coercion (coercion)

C. 3 y = (double) 5; // explicitly coercion (casting)

 

  1. They decrease the type error detection ability of the compiler, coercion is an automatic conversion into a legal type of an operands of an operator , it means if it’s legal type (can be read by the system) , it won’t be considered as a type error. For example, suppose a program had the int variables a and b and the float variable d. Now, if a programmer meant to type a + b, but mistakenly typed a + d, the error would not be detected by the compiler. The value of a would simply be coerced to float. So, the value of strong typing is weakened by coercion.
This entry was posted in Programming Language Concept. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *