LZN's Blog CodePlayer

Scratch Notes for Stanford CS107: Programming Paradigm Ch01-02

2020-04-26
LZN

L1: Administrative Details

Notes

  • C
  • Assambly
  • C++
  • Concurrent Programming
  • Scheme
  • Python

C is more procedure-oriented, from main to sub function, and sub function to sub-sub function. “Procedure Paradiam”

Concurrent programming issue: The shared resource that two different processes to access to.

Scheme is in functional paradiam: rely on a return from a function to move forward! Thus you can program without side effects.

e.g. side effects in C/C++: Pass a data structure into a function by reference, and change it in the function, when return, it changed.

Python, with good web libraries.

Qustions

  • Here talking about concurrent programming in one CPU? Seemingly in a Time-sharing system.

Glossary

handouts 讲义   mileage 里程    pertinent 相关的    synopsis 概要   assembly code 汇编代码
asterisks 星号  ampersands 连字号   miniature 微型  

L2: C/C++ Data Types - Interpretations

Notes

Low-level memory mechanisms.

C/C++ Data types |Data Type|Length| |—-|—-| |bool|1 byte| |char|1 byte| |short|2 byte| |int|4 byte*| |long|4 byte| |float|4 byte| |double|8 byte|

Technically, a Boolean can be mapped by a single bit in memory. Can engineer the compiler to do so.

Considering sign, the first bit in 1 — minus sign.

When 0000 0000 0000 0111 + 1000 0000 0000 0111, if the first bit is sign, the result is not right. To deal with this problem, use invert + 1 to represent negative values. Two’s complement(补码)

char ch = 'A';
short s = ch;
cout << s << endl;
OUTPUT:
65
ch 1111 65
s 1111 65
int i = 2^21+2^10+2^1
short s = i

OUTPUT:
1026
short s = -1;
int i = s;
s 1111 1111 1111 1111    
i 1111 1111 1111 1111 1111 1111 1111 1111

Floating things are wierd. See IEEE 754

Single (32 bits/ 4 bytes):

(-1)^sign(1.b22b21…b0)2^(e-127)

Double (64 bits/8 bytes):

Qustions

Precision varies with the magnitude of the represented number:

Single Precision: 10^N~10e(N-8) Double Precision: 10^N-10e(N-16)

int i =5;
float f=i;
cout<<f<<endl;

5=>5.0=1.25*2^e # Totally different!

Be careful with the precision loss when convert int to float!

Double makes things better, merely better.

Glossary


Updated 2020-04-26


Comments

Content