[Contiki-developers] Including math.h in contiki

1,476 views
Skip to first unread message

Aborigines

unread,
Sep 18, 2011, 8:38:30 PM9/18/11
to contiki-d...@lists.sourceforge.net
Hi,all. I find in the previous contiki-developers mailing list that there's been someone including math.h. He added a "-lm" at somewhere in Makefile.include and he said it helped mathmatic function work fine. But for me, it doesn't change anything. Cooja still can't recognize the mathmatic function. Hoping help from here.

Best regards
Li


------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the
BlackBerry® mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry® DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1
_______________________________________________
Contiki-developers mailing list
Contiki-d...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/contiki-developers

Aborigines

unread,
Sep 19, 2011, 8:36:45 AM9/19/11
to contiki-d...@lists.sourceforge.net
I have included the math.h. I find that when I make calculation with integer in printf it,
it is a right answer but when I printf float or double, the result is wrong?
does anybody have some experience about it?


> Date: Mon, 19 Sep 2011 08:38:30 +0800 (CST)
> From: Aborigines <kid...@mail.ustc.edu.cn>
> Subject: [Contiki-developers] Including math.h in contiki
> To: contiki-d...@lists.sourceforge.net
> Message-ID: <21968167.1085991316392710985.JavaMail.coremail@mailweb>
> Content-Type: text/plain; charset=us-ascii

Mohammad Abdellatif

unread,
Sep 20, 2011, 6:54:32 AM9/20/11
to Contiki developer mailing list
in your Makefile, instead of
LDFLAGS+=-lm
you should specify
TARGET_LIBFILES += -lm
and it will make msp430-gcc happy.

Mohammad Abdellatif

Phd student at Faculty of Engineering University of Porto (FEUP)
Researcher at Inesc Porto, UTM, WiN 

Peter Bigot

unread,
Sep 20, 2011, 8:15:07 AM9/20/11
to Contiki developer mailing list
And if you're using msp430-libc, I believe printf does not support
floating point numbers.

Peter

> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure contains a
> definitive record of customers, application performance, security
> threats, fraudulent activity and more. Splunk takes this data and makes
> sense of it. Business sense. IT sense. Common sense.
> http://p.sf.net/sfu/splunk-d2dcopy1


> _______________________________________________
> Contiki-developers mailing list
> Contiki-d...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/contiki-developers
>
>

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1

Mohammad Abdellatif

unread,
Sep 20, 2011, 8:17:46 AM9/20/11
to Contiki developer mailing list
And if you're using msp430-libc, I believe printf does not support
floating point numbers.

True.. i tried everything and still couldnt print them


Mohammad Abdellatif

Phd student at Faculty of Engineering University of Porto (FEUP)
Researcher at Inesc Porto, UTM, WiN 




Aborigines

unread,
Sep 20, 2011, 9:27:31 AM9/20/11
to contiki-d...@lists.sourceforge.net
I have come up a solution like this:I find that if you do some calculation and store the result to a double or float variable,
this variable actually stores it correctly. The problem is just that msp430-gcc can't display it correctly.
So I try to use int variable to print out the double or float variable. And it works fine to me.

> Date: Tue, 20 Sep 2011 13:17:46 +0100
> From: Mohammad Abdellatif <moh.abd...@gmail.com>
> Subject: Re: [Contiki-developers] Including math.h in contiki
> To: Contiki developer mailing list
> <contiki-d...@lists.sourceforge.net>
> Message-ID:
> <CAH5uiRCEaqBEwFjyom2sX9=FxNyX-j6QMdO4kd9FJ8kkn=7m...@mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"


>
> And if you're using msp430-libc, I believe printf does not support
> floating point numbers.
>
> True.. i tried everything and still couldnt print them
>

> *Mohammad Abdellatif*
> *
> *
> *Phd student at Faculty of Engineering University of Porto (FEUP)*
> *Researcher at Inesc Porto, UTM, WiN *
> *http://win.inescporto.pt/mma* <http://win.inescporto.pt/mma>

> -------------- next part --------------
> An HTML attachment was scrubbed...
>
> ------------------------------


>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure contains a
> definitive record of customers, application performance, security
> threats, fraudulent activity and more. Splunk takes this data and makes
> sense of it. Business sense. IT sense. Common sense.
> http://p.sf.net/sfu/splunk-d2dcopy1
>

> ------------------------------


>
> _______________________________________________
> Contiki-developers mailing list
> Contiki-d...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/contiki-developers
>
>

> End of Contiki-developers Digest, Vol 64, Issue 94
> **************************************************

Eduardo Montoya

unread,
Sep 21, 2011, 6:11:28 AM9/21/11
to Contiki developer mailing list
What I do is to cast to int in the printf function itself, and
multiply by a power of 10 if I want more resolution:

printf("Some float value = %d/n", (int)(1000*floatVariabe));

--
Eduardo Montoya Marín
---------------------------------------
CEDINT - UPM
Campus de Montegancedo
28223 Pozuelo de Alarcón
Madrid - ESPAÑA
Tlf: +34 91 336 45 13
Fax: +34 91 336 45 01
http://www.cedint.upm.es
---------------------------------------

2011/9/20 Aborigines <kid...@mail.ustc.edu.cn>:

Marios Koutroullos

unread,
Oct 4, 2011, 3:48:10 AM10/4/11
to Contiki developer mailing list
How to display the float if the integer part is too big. For example:

float f_num = 50.0*50.0*20.0;

How can I display this number. I try to printf with %d and casting the
f_num to (int) but I get -15530.

Any suggestions?

>This message has been scanned for viruses and
>dangerous content by MailScanner, and is
>believed to be clean.

Marios Koutroullos

unread,
Oct 4, 2011, 4:29:10 AM10/4/11
to Contiki developer mailing list
I found out that if I print the following float by casting it to int it
prints correctly:
float f = 32000.0
printf(" %d \n", (int)f);

However if I try to print the following float I get negative:
float f = 33000.0
printf(" %d \n", (int)f);

How I can display float numbers greater than 33000.0 using msp430-gcc?

Moritz Struebe

unread,
Oct 4, 2011, 5:08:39 AM10/4/11
to contiki-d...@lists.sourceforge.net
On 2011-10-04 10:29, Marios Koutroullos wrote:
> I found out that if I print the following float by casting it to int it
> prints correctly:
> float f = 32000.0
> printf(" %d \n", (int)f);
>
> However if I try to print the following float I get negative:
> float f = 33000.0
> printf(" %d \n", (int)f);
>
>
>
> How I can display float numbers greater than 33000.0 using msp430-gcc?
>

This has nothing to do with msp430 but casting in C, integer-width and
how printf works (it it not Java!). You should grab a decent C-book (one
of the thicker ons) and read the chapter about casting, as it is likely
that you will run into problems at some other places, too.

Cheers
Morty


--
Dipl.-Ing. Moritz 'Morty' Struebe (Wissenschaftlicher Mitarbeiter)
Lehrstuhl für Informatik 4 (Verteilte Systeme und Betriebssysteme)
Friedrich-Alexander-Universität Erlangen-Nürnberg
Martensstr. 1
91058 Erlangen

Tel : +49 9131 85-25419
Fax : +49 9131 85-28732
eMail : str...@informatik.uni-erlangen.de
WWW : http://www4.informatik.uni-erlangen.de/~morty


Marios Koutroullos

unread,
Oct 4, 2011, 5:25:12 AM10/4/11
to Contiki developer mailing list
I tried casting to "unsigned long int" and again I have the same problem.
So it must be a printf problem...

float f_num = 33000.0;

printf("%d\n",(unsigned long int)f_num);

On 10/4/11 12:08 PM, "Moritz Struebe"
<Moritz....@informatik.uni-erlangen.de> wrote:

>--------------------------------------------------------------------------
>----
>All the data continuously generated in your IT infrastructure contains a
>definitive record of customers, application performance, security
>threats, fraudulent activity and more. Splunk takes this data and makes
>sense of it. Business sense. IT sense. Common sense.

------------------------------------------------------------------------------

Moritz Struebe

unread,
Oct 4, 2011, 5:30:23 AM10/4/11
to Contiki developer mailing list
Because it's not java... %d is an integer - not an unsigned integer......

Cheers
Morty

Matthias Philipp

unread,
Oct 4, 2011, 5:30:57 AM10/4/11
to Contiki developer mailing list
Hi Marios,

try 'man 3 printf' and have a look at the length modifier 'l' as well as
the conversion specifier 'u'.

Cheers, Matthias

David Kopf

unread,
Oct 4, 2011, 8:24:15 AM10/4/11
to Contiki developer mailing list
Many 8 bit gcc's have a problem with printf of floats in the default
small-footprint configuration. Floating point is not enabled and the printf
library routines may not unpack floats and long longs correctly so they
spill over into the next field or after the end of the formatted text. That
can be fixed by including the bigger libraries but the sky already has
RAM/ROM memory limits, so do you *really* need floating point? The main use
is to give fixed precision over a large dynamic range. If you just want nice
prints like 1.23 do the calculation in milliunits and then e.g.
printf("Battery =%u.%2u
volts\n",millivolts/1000,millivolts-1000*(millivolts/1000));

If you need exponentiation or square roots it gets trickier; those can still
be done (and more efficiently) in integer arithmetic but you may need to
extract the isqrt routines from the library or you will end up with the
entire floating point library anyway.


-----Original Message-----
From: Marios Koutroullos
Sent: Tuesday, October 04, 2011 5:25 AM
To: Contiki developer mailing list
Subject: Re: [Contiki-developers] Including math.h in contiki

Marios Koutroullos

unread,
Oct 5, 2011, 5:01:17 AM10/5/11
to Contiki developer mailing list
Actually what I am trying do is send the node position of a node from
COOJA to node memory by writing in it memory (position is java side is
double type), do some calculations in node side which include square,
square root, multiplication, divisions .... to calculate a target
position for node and then COOJA will read back from memory of node the
new position and relocate the node.

So copy the position from java to memory of node successfully. My problem
is that if I try to do some debugging at C side for the calculations I
can not understand anything because printing of floats is not working
correctly.

What is the best workaround?

>--------------------------------------------------------------------------
>----
>All the data continuously generated in your IT infrastructure contains a
>definitive record of customers, application performance, security
>threats, fraudulent activity and more. Splunk takes this data and makes
>sense of it. Business sense. IT sense. Common sense.

David Kopf

unread,
Oct 5, 2011, 8:09:44 AM10/5/11
to Contiki developer mailing list
How about using longs to calculate in millimeters, that would work up to
several kilometers. Squares might require long long but that does not have
to be printed.
Reply all
Reply to author
Forward
0 new messages