ichirin2501's diary

いっちりーん。

Square root

記事にするつもりはなかったけど、発見があったのでメモ代わりに。

Square root

Square root

//75byte
#import<stdlib.h>
main(i)
{
  for(;gets(&i);)printf("%.9f\n",sqrt(atof(&i)));
}

70byteが切れないorz
atoi関数はstdlib.hをインクルードしなくてもgccコンパイラが勝手に色々やってくれて動作しますが、
atof関数はちゃんとインクルードしないと動作しないご様子。
でもコンパイルは通るんだよね…なんでだろ。


文字列を数値に変換する関数を調べてみると、


int atoi (const char *p);
long atol (const char *p);
long strtol (const char *nptr, char **endptr, int base);
long long int atoll (const char *p);
long long int strtoll (const char *restrict p, char **restrict endp, int base);
float strtof (const char *restrict p, char **restrict endp);
double atof (const char *p);
double strtod (const char *nptr, char **endptr);
long double strtold (const char *restrict p, char **restrict endp);
unsigned long strtoul (const char *nptr, char **endptr, int base);
unsigned long long int strtoull(const char *restrict p, char **restrict endp, int base);
ちなみに全部stdlib.hです。
atof関数はfだからfloat型を返すものだと思ってましたがdoubleでびっくりw



今回atof関数を使用して気付いたことは、0xA8のような16進数の文字列を引数として渡しても動作すること。


input
0x08
0x09
0x0A
0x0B
0x0F
0xFF
0xFFF
0x123
0x123.fed
07
010
011

output
8.000000
9.000000
10.000000
11.000000
15.000000
255.000000
4095.000000
291.000000
291.995361
7.000000
10.000000
11.000000

16進表記は正しく動作しますが、8進表記はただの10進数として読み取られるようです。


追記:
strtof() , strtod()関数でも同様に16進数表記は読み取る。