Discussion:
Float Variables
(too old to reply)
mridul.eecs@yahoo.com [nuttx]
2017-12-18 10:37:48 UTC
Permalink
I was trying to edit an example on accelerometer. I am unable to typecast int16 values of the acceleration to float value. i need to calculate acceleration in m/s^2, for that i need to convert it to float i dont know where am i going wrong in i tried to typecast like

accz = (float)acc_z;
printf("accz = %f\n",accz);
value printing is zero. when i tried ir without typecasting it gave me expected printf results.


Please help.
spudarnia@yahoo.com [nuttx]
2017-12-18 10:59:23 UTC
Permalink
Most likely an issue with %f. Try %8.2f
mridul.eecs@yahoo.com [nuttx]
2017-12-18 11:22:15 UTC
Permalink
didn't worked.
Oleg Evseev ev.mipt@gmail.com [nuttx]
2017-12-18 12:13:35 UTC
Permalink
Try cast it to double instead of float.
Post by ***@yahoo.com [nuttx]
didn't worked.
------------------------------
------------------------------
Reply via web post
<https://groups.yahoo.com/neo/groups/nuttx/conversations/messages/16933;_ylc=X3oDMTJyajN0NXY1BF9TAzk3MzU5NzE0BGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRtc2dJZAMxNjkzMwRzZWMDZnRyBHNsawNycGx5BHN0aW1lAzE1MTM1OTYxMzY-?act=reply&messageNum=16933>
• Reply to sender
New Topic
<https://groups.yahoo.com/neo/groups/nuttx/conversations/newtopic;_ylc=X3oDMTJmb21uOTZrBF9TAzk3MzU5NzE0BGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRzZWMDZnRyBHNsawNudHBjBHN0aW1lAzE1MTM1OTYxMzY->
• Messages in this topic
<https://groups.yahoo.com/neo/groups/nuttx/conversations/topics/16930;_ylc=X3oDMTM3b3J0OWU0BF9TAzk3MzU5NzE0BGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRtc2dJZAMxNjkzMwRzZWMDZnRyBHNsawN2dHBjBHN0aW1lAzE1MTM1OTYxMzYEdHBjSWQDMTY5MzA->
(3)
------------------------------
Have you tried the highest rated email app? <https://yho.com/1wwmgg>
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email
app on the market. What are you waiting for? Now you can access all your
inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email
again with 1000GB of free cloud storage.
------------------------------
Visit Your Group
<https://groups.yahoo.com/neo/groups/nuttx/info;_ylc=X3oDMTJmbWZyaXJwBF9TAzk3MzU5NzE0BGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRzZWMDdnRsBHNsawN2Z2hwBHN0aW1lAzE1MTM1OTYxMzY->
- New Members
<https://groups.yahoo.com/neo/groups/nuttx/members/all;_ylc=X3oDMTJnNnM0c2g3BF9TAzk3MzU5NzE0BGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRzZWMDdnRsBHNsawN2bWJycwRzdGltZQMxNTEzNTk2MTM2>
3
[image: Yahoo! Groups]
<https://groups.yahoo.com/neo;_ylc=X3oDMTJlaGJlMmw0BF9TAzk3NDc2NTkwBGdycElkAzIzMzg5MDcwBGdycHNwSWQDMTcwNTAwNjU1OQRzZWMDZnRyBHNsawNnZnAEc3RpbWUDMTUxMzU5NjEzNg-->
• Privacy <https://info.yahoo.com/privacy/us/yahoo/groups/details.html> •
of Use <https://info.yahoo.com/legal/us/yahoo/utos/terms/>
.
__,_._,
Michael Smith drziplok@gmail.com [nuttx]
2017-12-20 05:56:29 UTC
Permalink
You haven’t provided enough information here. In particular, which CPU are you compiling for, and how? Do you have floating point support for printf enabled? (It’s big and you probably don’t want it). What do you mean by “tried ir without typecasting”? Did you pass your int16 argument (I assume you mean int16_t) to printf with the %f format specifier? That won’t give you an “expected” result. Do you mean that you performed the conversion without an explicit cast, i.e.

int16_t acc_z = something();
float accz = acc_z;

You might also try printing both values using %08x (don’t cast them) to see their binary representations.

= Mike
Post by ***@yahoo.com [nuttx]
I was trying to edit an example on accelerometer. I am unable to typecast int16 values of the acceleration to float value. i need to calculate acceleration in m/s^2, for that i need to convert it to float i dont know where am i going wrong in i tried to typecast like
accz = (float)acc_z;
printf("accz = %f\n",accz);
value printing is zero. when i tried ir without typecasting it gave me expected printf results.
Please help.
Mridul Pandey mridul.eecs@yahoo.com [nuttx]
2017-12-21 05:03:30 UTC
Permalink
Hi Mike,I printed all integer, float and double variables of the same value.. Integer is printing fine, neither is float or double.

On Wednesday, December 20, 2017 11:27 AM, "Michael Smith ***@gmail..com [nuttx]" <***@yahoogroups.com> wrote:


  You haven’t provided enough information here. In particular, which CPU are you compiling for, and how? Do you have floating point support for printf enabled? (It’s big and you probably don’t want it). What do you mean by “tried ir without typecasting”? Did you pass your int16 argument (I assume you mean int16_t) to printf with the %f format specifier? That won’t give you an “expected” result. Do you mean that you performed the conversion without an explicit cast, i.e.
int16_t acc_z = something(); float accz = acc_z;

You might also try printing both values using %08x (don’t cast them) to see their binary representations. 
 = Mike

On 18 Dec 2017, at 2:37 am, ***@yahoo.com [nuttx] <***@yahoogroups.com> wrote:
I was trying to edit an example on accelerometer. I am unable to typecast int16 values of the acceleration to float value. i need to calculate acceleration in m/s^2, for that i need to convert it  to float i dont know where am i going wrong in i tried to typecast like 
accz = (float)acc_z;printf("accz = %f\n",accz);value printing is zero.  when i tried ir without typecasting it gave me expected printf results.
Please help.



#yiv4923074372 #yiv4923074372 -- #yiv4923074372ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv4923074372 #yiv4923074372ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv4923074372 #yiv4923074372ygrp-mkp #yiv4923074372hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv4923074372 #yiv4923074372ygrp-mkp #yiv4923074372ads {margin-bottom:10px;}#yiv4923074372 #yiv4923074372ygrp-mkp .yiv4923074372ad {padding:0 0;}#yiv4923074372 #yiv4923074372ygrp-mkp .yiv4923074372ad p {margin:0;}#yiv4923074372 #yiv4923074372ygrp-mkp .yiv4923074372ad a {color:#0000ff;text-decoration:none;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ygrp-lc {font-family:Arial;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ygrp-lc #yiv4923074372hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ygrp-lc .yiv4923074372ad {margin-bottom:10px;padding:0 0;}#yiv4923074372 #yiv4923074372actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv4923074372 #yiv4923074372activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv4923074372 #yiv4923074372activity span {font-weight:700;}#yiv4923074372 #yiv4923074372activity span:first-child {text-transform:uppercase;}#yiv4923074372 #yiv4923074372activity span a {color:#5085b6;text-decoration:none;}#yiv4923074372 #yiv4923074372activity span span {color:#ff7900;}#yiv4923074372 #yiv4923074372activity span .yiv4923074372underline {text-decoration:underline;}#yiv4923074372 .yiv4923074372attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv4923074372 .yiv4923074372attach div a {text-decoration:none;}#yiv4923074372 .yiv4923074372attach img {border:none;padding-right:5px;}#yiv4923074372 .yiv4923074372attach label {display:block;margin-bottom:5px;}#yiv4923074372 .yiv4923074372attach label a {text-decoration:none;}#yiv4923074372 blockquote {margin:0 0 0 4px;}#yiv4923074372 .yiv4923074372bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv4923074372 .yiv4923074372bold a {text-decoration:none;}#yiv4923074372 dd.yiv4923074372last p a {font-family:Verdana;font-weight:700;}#yiv4923074372 dd.yiv4923074372last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv4923074372 dd.yiv4923074372last p span.yiv4923074372yshortcuts {margin-right:0;}#yiv4923074372 div.yiv4923074372attach-table div div a {text-decoration:none;}#yiv4923074372 div.yiv4923074372attach-table {width:400px;}#yiv4923074372 div.yiv4923074372file-title a, #yiv4923074372 div.yiv4923074372file-title a:active, #yiv4923074372 div.yiv4923074372file-title a:hover, #yiv4923074372 div.yiv4923074372file-title a:visited {text-decoration:none;}#yiv4923074372 div.yiv4923074372photo-title a, #yiv4923074372 div.yiv4923074372photo-title a:active, #yiv4923074372 div.yiv4923074372photo-title a:hover, #yiv4923074372 div.yiv4923074372photo-title a:visited {text-decoration:none;}#yiv4923074372 div#yiv4923074372ygrp-mlmsg #yiv4923074372ygrp-msg p a span.yiv4923074372yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv4923074372 .yiv4923074372green {color:#628c2a;}#yiv4923074372 .yiv4923074372MsoNormal {margin:0 0 0 0;}#yiv4923074372 o {font-size:0;}#yiv4923074372 #yiv4923074372photos div {float:left;width:72px;}#yiv4923074372 #yiv4923074372photos div div {border:1px solid #666666;min-height:62px;overflow:hidden;width:62px;}#yiv4923074372 #yiv4923074372photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv4923074372 #yiv4923074372reco-category {font-size:77%;}#yiv4923074372 #yiv4923074372reco-desc {font-size:77%;}#yiv4923074372 .yiv4923074372replbq {margin:4px;}#yiv4923074372 #yiv4923074372ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv4923074372 #yiv4923074372ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv4923074372 #yiv4923074372ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv4923074372 #yiv4923074372ygrp-mlmsg select, #yiv4923074372 input, #yiv4923074372 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv4923074372 #yiv4923074372ygrp-mlmsg pre, #yiv4923074372 code {font:115% monospace;}#yiv4923074372 #yiv4923074372ygrp-mlmsg * {line-height:1.22em;}#yiv4923074372 #yiv4923074372ygrp-mlmsg #yiv4923074372logo {padding-bottom:10px;}#yiv4923074372 #yiv4923074372ygrp-msg p a {font-family:Verdana;}#yiv4923074372 #yiv4923074372ygrp-msg p#yiv4923074372attach-count span {color:#1E66AE;font-weight:700;}#yiv4923074372 #yiv4923074372ygrp-reco #yiv4923074372reco-head {color:#ff7900;font-weight:700;}#yiv4923074372 #yiv4923074372ygrp-reco {margin-bottom:20px;padding:0px;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ov li a {font-size:130%;text-decoration:none;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv4923074372 #yiv4923074372ygrp-sponsor #yiv4923074372ov ul {margin:0;padding:0 0 0 8px;}#yiv4923074372 #yiv4923074372ygrp-text {font-family:Georgia;}#yiv4923074372 #yiv4923074372ygrp-text p {margin:0 0 1em 0;}#yiv4923074372 #yiv4923074372ygrp-text tt {font-size:120%;}#yiv4923074372 #yiv4923074372ygrp-vital ul li:last-child {border-right:none !important;}#yiv4923074372
spudarnia@yahoo.com [nuttx]
2017-12-21 12:26:35 UTC
Permalink
Post by Michael Smith ***@gmail.com [nuttx]
Do you have floating point support for printf enabled? (It’s big and you probably don’t want it).
Mike is asking if you have CONFIG_LIBC_FLOATINGPOINT=y in your .config file. By default floating point output is disabled and would account for the issues you are seeing.

Greg
Michael Smith drziplok@gmail.com [nuttx]
2017-12-22 05:17:53 UTC
Permalink
Your answer isn’t precise enough to help, sorry.

“I printed” does not describe what you did, and “fine” / “not fine” doesn’t describe the output.

Nothing you’ve said suggests you took my suggestion to use the %x format to examine the binary representation of the float values.

You still haven’t told us which CPU you’re compiling for, and how.

You still haven’t told us whether you have the floating point support for printf enabled.

You also haven’t explained why you think printf is the right way to test your code in the first place (hint: it’s usually not).

= Mike
Post by Mridul Pandey ***@yahoo.com [nuttx]
Hi Mike,
I printed all integer, float and double variables of the same value. Integer is printing fine, neither is float or double.
You haven’t provided enough information here. In particular, which CPU are you compiling for, and how? Do you have floating point support for printf enabled? (It’s big and you probably don’t want it).. What do you mean by “tried ir without typecasting”? Did you pass your int16 argument (I assume you mean int16_t) to printf with the %f format specifier? That won’t give you an “expected” result. Do you mean that you performed the conversion without an explicit cast, i.e.
int16_t acc_z = something();
float accz = acc_z;
You might also try printing both values using %08x (don’t cast them) to see their binary representations.
= Mike
Post by ***@yahoo.com [nuttx]
I was trying to edit an example on accelerometer. I am unable to typecast int16 values of the acceleration to float value. i need to calculate acceleration in m/s^2, for that i need to convert it to float i dont know where am i going wrong in i tried to typecast like
accz = (float)acc_z;
printf("accz = %f\n",accz);
value printing is zero. when i tried ir without typecasting it gave me expected printf results.
Please help.
Continue reading on narkive:
Loading...