Netduino home hardware projects downloads community

Jump to content

The Netduino forums have been replaced by new forums at This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

float vs. double in code

  • Please log in to reply
2 replies to this topic

#1 Gauss



  • Members
  • PipPip
  • 14 posts

Posted 09 October 2013 - 04:51 PM

So, I am somewhat curious about the backend implementation of the .NET MicroFramework.


I am programming a temperature monitoring system for my house and using the Netduino to do all the interpretation and logic to drive everything.


I have been using floats since my temperature sensor is only accurate to ~0.1 degrees, my issue is AnalogInput, most of the built in Math library functions, and any deicmal number I type in the program seems to use use doubles, which I have been casting as floats for my math.


Is this actually saving me anything? Since we have a 32-bit uC, I presume this is saving a memory location and register for operations with my program because double is 64-bit, but for all I know I am adding an operation without gaining anything because all the calculations will still be done using double.


I have a code snippet below of what I am talking about:

	    private static float GetTemp()	    {		    return (((float)0.442368) * (float)_adc.Read()) - ((float)4.92304);	    }

0.442368, 4.92304, and _adc.Read() are being treated as doubles according to the IDE, and I am casting as a float.

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 10 October 2013 - 12:03 PM

Hello Gauss.

In the "regular" .Net framework, the float type (i.e. System.Single) is used always lesser. It's right that it takes just 4 bytes, but almost any device running .Net does not have problems of ram, and mostly embeds a coprocessor which operates on 80-bits FP numbers.

The Micro Framework is perhaps the only version of .Net which deals with ram and processor speed, but the usage of this framework is totally irrelevant when compared to the millions of pc and phones.


I personally think the double-choice other than the float is correct, when considered the compatibility of the code. I like more taking my source and running on a pc and on a Netduino with no- or minimal-adjustments.

Also consider that the internal stack should issue as less as possible of rounding/FP errors, thus the max precision is preferable.


So, avoid to cast to a float even the source wouldn't require such a precision: simple take as is it!

Note that the same thing is for the integer domain, where the framework bases every operation on Int32.



Biggest fault of Netduino? It runs by electricity.

#3 Gauss



  • Members
  • PipPip
  • 14 posts

Posted 10 October 2013 - 03:12 PM

Mario, thank you for your response.


I was actually just reading your blog on performance metrics with code in the .Net Micro Framework.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.