LoginSignup
0
1

More than 5 years have passed since last update.

組込みC > atan2() > xが0.0の戻り値 > 3.40282346E+38 > 定義域エラーが発生した場合、関数は処理系定義の値を返す

Last updated at Posted at 2017-05-10
動作環境
とある16bitのMCU用開発環境 (C言語)

用意されたmath.hのatan2()を使用。

関連 http://www.c-tipsref.com/reference/math/atan2.html

tan^{-1}(y/x)

ここで、xが0.0の時の処理はどうなっているか。

ideone

#include <stdio.h>
#include <math.h>

int main(void) {

    printf("%f\n", atan2(0.0, 0.0));

    return 0;
}
run
0.000000

使用中の組込みC

3.40282346E+38が返るようになっていた。

結果としてprintf()表示した時にinfとなる。

使用している機器が故障して、データが取得できなくなった場合などはxが0.0になる場合がある。
その時に使用中の組込みCのライブラリでは、きちんと対処をしないとinfになるようだ。

他の開発環境・ライブラリの場合でx=0.0のatan2()戻り値がどうなるかは未調査。

atan2()を使う前に常にx=0.0のチェックを入れるのは安全ではある。

ATAN2(3)

http://man7.org/linux/man-pages/man3/atan2.3.html
RETURN VALUE の項目に色々な場合の戻り値がある。

If y is +0 (-0) and x is -0, +pi (-pi) is returned.
If y is +0 (-0) and x is +0, +0 (-0) is returned.

使用中の組込みCではこのような戻り値ではない。

教えていただいた事項

@SaitoAtsushi さんのコメントにて処理系定義の値が返っているようであることが分かりました。

情報感謝です。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1