Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ type
Q: What is the difference between typedef & Macros?

Answer:
Typedef is used to create a new name to an already existing data type. Redefine the name creates conflict with the previous declaration.

eg:
typedef unsigned int UINT32

Macros [#define] is a direct substitution of the text before compling the whole code. In the given example, its just a textual substitution. where there is a posibility of redifining the macro

eg:
#define chPointer char *
#undef chPointer
#define chPointer int *


[Quote}:http://www.geekinterview.com/question_details/57278

| BLOG TOP |
DATE: CATEGORY:C/C++ type

Single-precision 32 bit

A single-precision binary floating-point number is stored in 32 bits.

Bit values for the the IEEE 754 32bit float 0.15625
Bit values for the the IEEE 754 32bit float 0.15625

The exponent is biased by 28 − 1 − 1 = 127 in this case (Exponents in the range −126 to +127 are representable. See the above explanation to understand why biasing is done). An exponent of −127 would be biased to the value 0 but this is reserved to encode that the value is a denormalized number or zero. An exponent of 128 would be biased to the value 255 but this is reserved to encode an infinity or not a number (NaN). See the chart above.

For normalized numbers, which are the most common, the exponent is the biased exponent and fraction is the significand without the most significant bit.

The number has value v:

v = s × 2e × m

Where

s = +1 (positive numbers) when the sign bit is 0

s = −1 (negative numbers) when the sign bit is 1

e = Exp − 127 (in other words the exponent is stored with 127 added to it, also called "biased with 127")

m = 1.fraction in binary (that is, the significand is the binary number 1 followed by the radix point followed by the binary bits of the fraction). Therefore, 1 ≤ m < 2.

In the example shown above, the sign is zero, the exponent is −3, and the significand is 1.01 (in binary, which is 1.25 in decimal). The represented number is therefore +1.25 × 2−3, which is +0.15625.

Double-precision 64 bit

The three fields in a 64bit IEEE 754 float
The three fields in a 64bit IEEE 754 float

Double precision is essentially the same except that the fields are wider:

The fraction part is much larger, while the exponent is only slightly larger. The standard creators believed precision is more important than range.

NaNs and Infinities are represented with Exp being all 1s (2047). If the fraction part is all zero then it is Infinity, else it is NaN.

For Normalized numbers the exponent bias is +1023 (so e is exponent (− 1023)). For Denormalized numbers the exponent is (−1022) (the minimum exponent for a normalized number—it is not (−1023) because normalised numbers have a leading 1 digit before the binary point and denormalized numbers do not). As before, both infinity and zero are signed.


float型で表現できるのは、±10-38〜1038(2127)の範囲で、小数以下有効桁は7桁(2-23)です。
double型では、±10-308〜10308(21024)の範囲で、小数以下有効桁は15桁(2-52)です。

普通の計算ではfloat型で十分ですが、極端に大きな数を扱う場合や、高い精度が必要となる場合は、double型で宣言すると良いでしょう。

また、整数を扱うint型は、-2147483648〜2147483647の範囲です。
階乗の計算(例えば、5!=120、10!=3628800)のように計算結果が巨大な値になる場合は、注意が必要です。

型名 対象 ※1 表現できる範囲 有効桁
int 整数 %d -2147483648〜2147483647 ---
float 小数 %f ±10-38〜1038 7桁
double 小数 %f ±10-308〜10308 15桁


[Quote]:http://www.aso.ecei.tohoku.ac.jp/~yoshi/C/double.htmlhttp://en.wikipedia.org/wiki/IEEE_floating-point_standard

| BLOG TOP |
DATE: CATEGORY:C/C++ type
2バイトのchar
ANSI C は標準でワイド文字をサポートしている
ワイド文字とは、1文字表現するのに2バイト用いる文字のことを言います
同時に1バイト以上の文字のことをマルチバイト文字と言います

これまで、文字コードはASCIIコードを用いてきました
これは1文字を1バイト( char 型 )で表現できるコードで、世界的に標準のコードです
しかし、1バイトだけでは中国語や日本語のような漢字や記号を多く持つ言語を表せません
そこでワイド文字が使われるというわけです

ワイド文字は国際的なアプリケーションの開発には必要な知識です
ことUnicodeなどは非常に有名ですね

ワイド文字の型はwchar_tです
これは標準型として WCHAR.H ヘッダファイルに定義されています
//または STDDEF.H、 STDLIB.H ヘッダファイルなど

typedef unsigned short wchar_t;

wchar_t 型は unsigned short int すなわち16ビットです
当然、short int 型なので、通常の文字列関数などには使用できません

#include
#include

int main() {
wchar_t ch;
printf("%d" , sizeof ch);
return 0;
}

予想どおり、2という整数が標準出力に表示されます

次は、ワイド文字型で文字列を表現してみましょう
ワイド文字として文字列を格納するには、文字列の前にL(Long)プレフィックスを指定します
これは、コンパイラにワイド文字列として扱うように指示するものです

wchar_t wc[] = L"String...";

ワイド文字列は、必ず1文字2バイトの領域を確保します
Lとクォーテーションの間にホワイトスペースが入ってはいけません

#include
#include

int main() {
char ch[] = "Kitty on your lap";
wchar_t wch[] = L"Kitty on your lap";

printf("char 型文字列 = %d\n" , sizeof ch);
printf("wchar_t 型文字列 = %d\n" , sizeof wch);
return 0;
}

プログラムを実行すると、このような結果になりました

char 型文字列 = 18
wchar_t 型文字列 = 36

通常の char 型変数 ch 配列のサイズは、文字数 + 1 です(最後はヌル文字)
これに対し wchar_t 型変数 wch 配列は、(文字数 + 1) × 2 のサイズになります
ワイド文字はヌルも字も当然2バイトになるので注意していください

同様の方法で、wchar_t のポインタ型変数に文字列を代入することも可能です
また、単一のワイド文字の前に L プレフィックスを付けても間違いではありません

ほとんどの場合、wchar_t 型は 2バイト文字ですが
文字コードによっては、それ以上であることも考えられます
ワイド文字の文字コードの定義は ANSI C ではされていないので、実装依存になります

[Quote]:http://wisdom.sakura.ne.jp/programming/c/c63.html

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ