質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

関数

関数(ファンクション・メソッド・サブルーチンとも呼ばれる)は、はプログラムのコードの一部であり、ある特定のタスクを処理するように設計されたものです。

Q&A

解決済

1回答

1220閲覧

C言語 math.hのcos関数の戻り値が0.00になってしまう

Hayaaaaaa

総合スコア46

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

関数

関数(ファンクション・メソッド・サブルーチンとも呼ばれる)は、はプログラムのコードの一部であり、ある特定のタスクを処理するように設計されたものです。

0グッド

0クリップ

投稿2021/12/28 09:22

編集2021/12/29 06:56

コード

c

1const float angle_rad = (float)(SiderealTime - radians(longitude_deg)); 2// (中略 変数angle_radへの代入はない) 3Serial.print(angle_rad); // -4.96と表示 4Serial.print(cos(angle_rad)); // 0.00と表示 <-0.25と出てほしい 5Serial.print(cos(-4.96)); // 0.25と表示

前提・実現したいこと

C言語でmath.hのcos関数の戻り値が0.00になってしまう問題を解決したいです。

マイコンボード(GR-Citrus)で計算部分のプログラムを作成していたときに発生した問題です。
math.hのcos関数に変数を代入して計算させると戻り値が常に0.00になってしまい、目的の計算プログラムが機能しなくなり困っています。
cos(-4.96)が0.25を返しているのでcos関数自体は機能しているはずですし、Serial.print(angle_rad)でまともな数字を表示していますし、さっぱり何が原因であるのかがわかりません。

###動作環境
マイコンボード:GR-Citrus
開発環境:IDE for GR v1.13

発生している問題・エラーメッセージ

コンパイルエラーなどは出ませんでした。

試したこと

angle_rad変数に直接数字を代入して、実行してみました。するとcos関数は思い通りの計算結果を返しました。

c

1const float angle_rad = -4.96; // 直接-4.96を代入してみる 2// (中略 変数angle_radへの代入はない) 3Serial.print(angle_rad); // -4.96と表示 4Serial.print(cos(angle_rad)); // 0.25と表示(合っている)

おそらくコンパイルオプション

platform.txt

1# RX63N and platform. 2# ------------------------------ 3 4# For more info: 5 6name=Renesas RX63 7#version=2.2.0 8 9# AVR compile variables 10# --------------------- 11 12# Default "compiler.path" is correct, change only if you want to overidde the initial value 13compiler.path={runtime.ide.path}/hardware/tools/gcc-rx/rx-elf/rx-elf/bin/ 14compiler.ar.path = {runtime.ide.path}/hardware/arduino/rx63/cores/ 15compiler.c.cmd=rx-elf-gcc 16compiler.c.flags=-Wall -ffunction-sections -fno-function-cse -fsigned-char -fdata-sections -mno-balign -DARDUINO=100 -DCPPAPP -D__RX_LITTLE_ENDIAN__=1 -D__T4__ -O2 -flto -mlittle-endian-data -mcpu=rx600 -Ihardware/arduino/rx63/cores/ -Ihardware/arduino/rx63/cores/core -Ihardware/arduino/rx63/cores/core/avr -Ihardware/arduino/rx63/cores/lib -Ihardware/arduino/rx63/libraries/DSP -Ihardware/arduino/rx63/libraries/DSP/utility -Ihardware/arduino/rx63/libraries/EEPROM -Ihardware/arduino/rx63/libraries/EEPROM/utility -Ihardware/arduino/rx63/libraries/Ethernet -Ihardware/arduino/rx63/libraries/Ethernet/utility -Ihardware/arduino/rx63/libraries/Ethernet/utility/driver -Ihardware/arduino/rx63/libraries/Ethernet/utility/r_byteq_v1.30 -Ihardware/arduino/rx63/libraries/Ethernet/utility/r_config -Ihardware/arduino/rx63/libraries/Ethernet/utility/T4_src -Ihardware/arduino/rx63/libraries/Ethernet/utility/T4_src/checksum -Ihardware/arduino/rx63/libraries/Ethernet/utility/T4_src/checksum/rx -Ihardware/arduino/rx63/libraries/Firmata -Ihardware/arduino/rx63/libraries/LiquidCrystal -Ihardware/arduino/rx63/libraries/MsTimer2 -Ihardware/arduino/rx63/libraries/RTC -Ihardware/arduino/rx63/libraries/RTC/utility -Ihardware/arduino/rx63/libraries/SD -Ihardware/arduino/rx63/libraries/SD/utility -Ihardware/arduino/rx63/libraries/Servo -Ihardware/arduino/rx63/libraries/SoftwareSerial -Ihardware/arduino/rx63/libraries/SPI -Ihardware/arduino/rx63/libraries/Stepper -Ihardware/arduino/rx63/libraries/Wire -Ihardware/arduino/rx63/libraries/Wire/utility -Ihardware/arduino/rx63/cores/rx63n -Ihardware/arduino/rx63/libraries/ArduinoJson -Ihardware/arduino/rx63/libraries/postParser -Ihardware/arduino/rx63/libraries/PubSubClient -Ihardware/arduino/rx63/libraries/WavMp3p -Ihardware/arduino/rx63/libraries/WavMp3p/utility -Ihardware/arduino/rx63/libraries/WiFiEsp -Ihardware/arduino/rx63/libraries/WiFiEsp/utility -Ihardware/arduino/rx63/libraries/AdafruitGFX 17compiler.S.cmd=rx-elf-gcc 18compiler.S.flags= -ffunction-sections -fno-function-cse -fsigned-char -fdata-sections -mno-balign -DARDUINO=100 -DCPPAPP -D__RX_LITTLE_ENDIAN__=1 -D__T4__ -O2 -mlittle-endian-data -mcpu=rx600 19compiler.elf2hex.flags=-O binary 20#compiler.elf2hex.flags=-O srec 21compiler.elf2hex.cmd=rx-elf-objcopy 22compiler.c.elf.cmd=rx-elf-ld 23compiler.c.elf.flags= 24compiler.ar.cmd=rx-elf-ar 25compiler.ar.flags=rcs 26compiler.ld.flags= -e_PowerON_Reset -no-keep-memory -S 27compiler.size.cmd=rx-elf-size 28# this can be overriden in boards.txt 29build.extra_flags= 30 31# AVR compile patterns 32# -------------------- 33 34## Compile c files 35recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" -std=gnu99 {includes} {compiler.c.flags} {build.extra_flags} -c -x c "{source_file}" -o "{object_file}" 36 37## Compile c++ files 38recipe.cpp.o.pattern="{compiler.path}{compiler.c.cmd}" -std=gnu++11 {includes} {compiler.c.flags} {build.extra_flags} -c -x c++ "{source_file}" -o "{object_file}" 39 40## Compile S files 41recipe.S.o.pattern="{compiler.path}{compiler.c.cmd}" {includes} {compiler.S.flags} -c -x assembler-with-cpp "{source_file}" -o "{object_file}" 42 43## Create archives 44recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{build.path}/{archive_file}" "{object_file}" 45 46## for rapid compile 47## Combine gc-sections, archives, and objects 48recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.ld.flags} "-T{build.variant.path}/{build.ldscript}" {object_files} --whole-archive "{build.variant.path}/gr_common.a" --no-whole-archive "-L{build.path}" "{build.path}/{archive_file}" -o "{build.path}/{build.project_name}.elf" "-L{runtime.ide.path}/hardware/tools/gcc-rx/rx-elf/rx-elf/rx-elf/lib/64-bit-double" "-L{runtime.ide.path}/hardware/tools/gcc-rx/rx-elf/rx-elf/lib/gcc/rx-elf/4.8.4.201801-GNURX/64-bit-double" --no-flag-mismatch-warnings --start-group -lstdc++ -lnosys -lsim -lm -lc -lgcc --end-group 49 50## Create eeprom 51recipe.objcopy.eep.pattern= 52 53## Create hex 54recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin" 55 56## Compute size 57recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf" 58recipe.size.regex=^(?:.text|.data|.bootloader)\s+([0-9]+).* 59recipe.size.regex.data=^(?:.data|.bss|.noinit)\s+([0-9]+).* 60recipe.size.regex.eeprom=^(?:.eeprom)\s+([0-9]+).* 61 62 63# SAM3 Uploader tools 64# ------------------- 65 66tools.savesketch.cmd=SaveSketch 67tools.savesketch.cmd.windows=SaveSketch.bat 68tools.savesketch.path={runtime.ide.path}/hardware/tools/rx 69 70tools.savesketch.upload.params.verbose= 71tools.savesketch.upload.params.quiet= 72tools.savesketch.upload.pattern="{path}/{cmd}" "{name}" "{build.path}\{build.project_name}.bin"

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ak.n

2021/12/28 09:54

float を double にする、 もしくは、cos((double)angle_rad) とするとどうなりますか?
ozwk

2021/12/28 10:26 編集

コンパイラの最適化オプションはどうなってますか? 何かしら最適化が入っていた場合は最適化なしにしてどうなるか確認してください
Hayaaaaaa

2021/12/28 11:03 編集

doubleで変数宣言する、cos((double)angle_rad)、cos((float)angle_rad)を試しましたが、結果はどれも0.00となりました。 コンパイラのオプションはおそらくplatform.txtのファイルで設定されているのだと思いましたが、私の知識不足でその引数がどう影響を及ぼしているか検討も付きません。(質問本文に追記します)
guest

回答1

0

ベストアンサー

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Hayaaaaaa

2021/12/28 12:21

ありがとうございます、正しいsin cos tan の計算できました! 上記のURLからrx63.zipをダウンロードし、それをコンパイラのディレクトリと置き換えると上手くいきました。感謝です!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問