コード
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"
回答1件
あなたの回答
tips
プレビュー