jsk-ros-pkg / jsk_roseus

ROS EusLisp Client
http://wiki.ros.org/roseus/Tutorials
17 stars 56 forks source link

計算した値を四捨五入するのをやめられますか #535

Open kochigami opened 7 years ago

kochigami commented 7 years ago

pythonだと、 1.50199099766e+18 のように表示されるのですが、 euslispだと、 1.501991e+18 のようになります。

やりたいことは、 時間を記録して、ファイルに書き込むことなので、 今の所は計算をせず、sec, nsec両方取り出してファイルに書き込もうと思います。

4.E2-irteusgl$ setq time (ros::time-now)
#<ros::time #X57bc058 1502011442.658>
5.E2-irteusgl$ send time :sec
1502011442
6.E2-irteusgl$ send time :nsec
658569503
7.E2-irteusgl$ setq answer (+ (* (send time :sec) 1000000000.0) (send time :nsec))
1.502011e+18
8.E2-irteusgl$ answer
1.502011e+18
9.E2-irteusgl$ send time :now
#<ros::time #X57bc058 1502011531.816>
k-okada commented 7 years ago
$ (format t "~32,16f~%" (/ (send time :to-nsec) 1000000000.0))
     1502013373.6416673660278320

はどうでしょう.あとは、

$ setq answer (+ (* (send time :sec) 1000000000) (send time :nsec))
1502013373641667228
$ answer
1502013373641667228

というほうほうもあります. あとは、もとのanswerの計算法だと

$ (format t "~f" answer)
1502013373641666560.0nil
$ (format t "~32,12f" answer)
1502013373641666560.000000000000

でしょうか.

-- ◉ Kei Okada

2017年8月6日 18:35 Kanae Kochigami notifications@github.com:

pythonだと、 1.50199099766e+18 のように表示されるのですが、 euslispだと、 1.501991e+18 のようになります。

やりたいことは、 時間を記録して、ファイルに書き込むことなので、 今の所は計算をせず、sec, nsec両方取り出してファイルに書き込もうと思います。

4.E2-irteusgl$ setq time (ros::time-now)

<ros::time #X57bc058 1502011442.658>

5.E2-irteusgl$ send time :sec 1502011442 6.E2-irteusgl$ send time :nsec 658569503 7.E2-irteusgl$ setq answer (+ (* (send time :sec) 1000000000.0) (send time :nsec)) 1.502011e+18 8.E2-irteusgl$ answer 1.502011e+18 9.E2-irteusgl$ send time :now

<ros::time #X57bc058 1502011531.816>

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jsk-ros-pkg/jsk_roseus/issues/535, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeG3H0a-2Lv3cij7uf1VKxYwsyDI-Saks5sVYjsgaJpZM4Ourxb .

kochigami commented 7 years ago

ご教示ありがとうございました。

もう一点、もしよろしければご教示ください。 (format t "~32,16f~%" (/ (send a :to-nsec) 1000000000.0)) の値をそのまま別の変数に代入したいのですが、できますでしょうか。 今は、記録をまとめて、string メッセージにして、 トピックに乗せて、それをファイルに書くようにしています・・・

22.E4-irteusgl$ (setq time_and_str (format nil "~a, ~a" (format t "~32,16f~%" (/ (send a :to-nsec) 1000000000.0)) "hoge"))
     1502019885.0964641571044922
"nil, hoge"
23.E4-irteusgl$ time_and_str
"nil, hoge"
k-okada commented 7 years ago

$ (setq xxx (format nil "~32,16f" (/ (send time :to-nsec) 1000000000.0)) ) " 1502013373.6416673660278320" $ (print abc) " 1502013373.6416673660278320 " " 1502013373.6416673660278320 "

でどうでしょう.

-- ◉ Kei Okada

2017年8月6日 20:49 Kanae Kochigami notifications@github.com:

ご教示ありがとうございました。

もう一点、もしよろしければご教示ください。 (format t "~32,16f~%" (/ (send a :to-nsec) 1000000000.0)) の値をそのまま別の変数に代入したいのですが、できますでしょうか。 今は、記録をまとめて、string メッセージにして、 トピックに乗せて、それをファイルに書くようにしています・・・

22.E4-irteusgl$ (setq time_and_str (format nil "~a, ~a" (format t "~32,16f~%" (/ (send a :to-nsec) 1000000000.0)) "hoge")) 1502019885.0964641571044922 "nil, hoge" 23.E4-irteusgl$ time_and_str "nil, hoge"

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jsk-ros-pkg/jsk_roseus/issues/535#issuecomment-320502058, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeG3BkMNCpa7oXcOlhzmK6rOgLCYMjHks5sVahZgaJpZM4Ourxb .

kochigami commented 7 years ago

ご教示ありがとうございました。

format t/ nil の違いがいまいち分かっていないので、 勉強します。

k-okada commented 7 years ago

formant t は標準出力への書き出し nilは書き出さない どちらにせよformat関数はフォーマットされた文字を返すので、nilで何も標準させずに、変数に割り当てる と言うことをしています

2017年8月6日(日) 21:10 Kanae Kochigami notifications@github.com:

ご教示ありがとうございました。

format t/ nil の違いがいまいち分かっていないので、 勉強します。

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/jsk-ros-pkg/jsk_roseus/issues/535#issuecomment-320503037, or mute the thread https://github.com/notifications/unsubscribe-auth/AAeG3Nt4LMnhwlYCkNmSfR9f3U_m_Rauks5sVa0bgaJpZM4Ourxb .

--

◉ Kei Okada

kochigami commented 7 years ago

すみません、今度はファイルに書き込んだ値を、 プログラムで読み込むときにまとめられるのをやめたいのですが、方法が分かりません。

たくさんファイルに書いてある数の大きさを比べたいのですが、 まとめられてしまっていて、全て同じ大きさと出てしまい困っています。

setq time 1502154610.7192630767822266
1.502155e+09

(setq time (format nil "~32,16f" time))
"     1502154610.7192630767822266"
read-from-string time
1.502155e+09

tmp.l

    1502154610.7192630767822266 help_me
(with-open-file (s "tmp.l" :direction :input)(while (setq b (read s nil))(print b)))
1.502155e+09
help_me
nil
YoheiKakiuchi commented 7 years ago

以下のような感じで、プリントXXXe+ZZZと同じように表示されていても、 イコールではなくて、表示桁数を増やせば正しい値が入っているので、 formatで必要な桁数を書きだせば、読み込んだ後で比較できると思います。

(setq timea 1502154610.7192630767822266)
1.502155e+09
(setq str (format nil "~32,16f" timea))
"     1502154610.7192630767822266"
(setq timea-str (read-from-string str))
1.502155e+09

(setq timeb 1502154610.719264)
1.502155e+09
(setq str (format nil "~32,16f" timeb))
"     1502154610.7192640304565430"
(setq timeb-str (read-from-string str))
1.502155e+09

(= timeb timeb-str)
t
(= timea timea-str)
t
(= timea timeb)
nil
(= timea-str timeb-str)
nil
(- timea timeb)
-9.536743e-07
(- timea-str timeb-str)
-9.536743e-07