Difference between revisions of "User:UplinkCoder/VRP"

From D Wiki
Jump to: navigation, search
(Created page with "'''Value Range Propergation / safe truncation''' What I am going to write about is an imporatnt feature of D. Safe implicit truncation. One thing C is criticized for is it'...")
 
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Value Range Propergation / safe truncation'''
+
'''Value Range Propagation / safe truncation'''
  
What I am going to write about is an imporatnt feature of D.
+
What I am going to write about is an important feature of D.
  
 
Safe implicit truncation.
 
Safe implicit truncation.
Line 9: Line 9:
 
You can do things like : char c = 400;
 
You can do things like : char c = 400;
  
With luck you will get a like,
+
With luck you will get a ,
  
 
warning: overflow in implicit conversion.
 
warning: overflow in implicit conversion.
Line 18: Line 18:
 
"If it compiles, it's most likely correct."
 
"If it compiles, it's most likely correct."
  
That if why DMD will not compile code with narrowing conversions.
+
That is why DMD will not compile code with narrowing conversions.
  
Execpt: if it can proof that this conversion is safe.
+
Execpt if it can proof that this conversion is safe.
  
 
To do that it needs meta-information about expressions.
 
To do that it needs meta-information about expressions.
Line 30: Line 30:
 
That means the Expression ''u-2'' with the Variable u of type ubyte has [-2,253]
 
That means the Expression ''u-2'' with the Variable u of type ubyte has [-2,253]
  
the formula for minus is :
+
== the formula for minus is : ==
 +
 
  
 
result.max = lhs.max - rhs.min
 
result.max = lhs.max - rhs.min
  
 
result.min = lhs.min - rhs.max
 
result.min = lhs.min - rhs.max

Latest revision as of 17:30, 13 November 2014

Value Range Propagation / safe truncation

What I am going to write about is an important feature of D.

Safe implicit truncation.

One thing C is criticized for is it's relatively lax type system.

You can do things like : char c = 400;

With luck you will get a ,

warning: overflow in implicit conversion.

On modern compilers.

This is not compatible with D's phlisophy, "If it compiles, it's most likely correct."

That is why DMD will not compile code with narrowing conversions.

Execpt if it can proof that this conversion is safe.

To do that it needs meta-information about expressions.

This meta-information is the Range of Values this expression can have.

A ubyte for example can have the Values from 0 to 255 or [0,255]

That means the Expression u-2 with the Variable u of type ubyte has [-2,253]

the formula for minus is :

result.max = lhs.max - rhs.min

result.min = lhs.min - rhs.max