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.

C# help

C# convert

  • Please log in to reply
2 replies to this topic

#1 JBkey



  • Members
  • PipPip
  • 14 posts
  • LocationAveiro, Portugal

Posted 17 April 2015 - 01:39 PM




I'm stuck with variables format. I know this is a stupid question but I'm on it for 2 hours and I'm losing my mind.


I began in this programing stuff maybe 2 months ago, so I'm really new at it.


The problem is:


I have:

command.Data = new ushort[] { xx, yy, zz };

And I need to do:

ushort zz = System.Math.Tan(c1AngleX);

But the output of System.Math.Tan is "double", so I kinda can't have the "ushort" there.

I tried this

ushort zz = (Convert.ToUInt16 (System.Math.Tan(c1AngleX));

But I can't.




There's any way to convert "double" to "ushort"?



#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 17 April 2015 - 04:22 PM


The conversion is rather trivial:

ushort zz = (ushort)System.Math.Tan(c1AngleX);

However, I don't think the "ushort" is a convenient way to store the tangent values, unless they fall with a restrict angle interval.

Moreover, the "ushort" isn't a nice (nor common) type: the "unsigned" types lead to mistakes, unless you check for under/overflow.

Why don't you use "float" instead of "ushort"?


On the regular .Net (desktop) framework I'd use "double" instead of "float", but here you have a tiny board and the resources (CPU and RAM) are precious. Double takes 8 bytes and Single (float) takes 4. Furthermore, the float math should be natively supported, but the "double" not.



Biggest fault of Netduino? It runs by electricity.

#3 cr0w5t3r


    New Member

  • Members
  • Pip
  • 9 posts
  • LocationUK

Posted 10 May 2015 - 09:23 PM

I've been using .NET for about 3 1/2 years now and I've never even come across the ushort type. I don't normally have to be too concious of how many bytes a variable would take up though which might play a factor in my answer (Usually work on web applications).


I would basically second what Mario suggested in that you should probably just use a float if resource is going to be an issue else just return it natively as a double.


The code could be;

// You don't have to set the variable type if you use the 'var' keyword as
// it will just be set to whatever is returned from the method. In this case a double.
var zz = System.Math.Tan(12342);


float zx = (float)System.Math.Tan(12342);

Most of the time I have managed to get away with using the following types in my code;


  • int
  • float
  • double
  • decimal
  • string
  • bool
  • object



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.