18680

Int or Unsigned Int to float without getting a warning

Sometimes I have to convert from an unsigned integer value to a float. For example, my graphics engine takes in a SetScale(float x, float y, float z) with floats and I have an object that has a certain size as an unsigned int. I want to convert the unsigned int to a float to properly scale an entity (the example is very specific but I hope you get the point).

Now, what I usually do is:

unsigned int size = 5; float scale = float(size); My3DObject->SetScale(scale , scale , scale);

Is this good practice at all, under certain assumptions (see Notes)? Is there a better way than to litter the code with float()?

<strong>Notes:</strong> I cannot touch the graphics API. I have to use the SetScale() function which takes in floats. Moreover, I also cannot touch the size, it has to be an unsigned int. I am sure there are plenty of other examples with the same 'problem'. The above can be applied to any conversion that needs to be done and you as a programmer have little choice in the matter.

Answer1:

There is an implicit conversion from unsigned int to float, so the cast is strictly unnecessary.

If your compiler issues a warning, then there isn't really anything wrong with using a cast to silence the warning. Just be aware that if size is very large it may not be representable exactly by a float.

Answer2:

My preference would be to use static_cast:

float scale = static_cast<float>(size);

but what you are doing is functionally equivalent and fine.

Recommend

  • R - subset data if conditions
  • Whats wrong with this C shell script?
  • UI router not working
  • Creating a checkmark inside a Pickerview [duplicate]
  • How do I make a SKSpriteNode that does not respond to touch if it's pixels are transparent?
  • Which way to go with graphic-intense multi-touch app, XNA or WPF?
  • functions and when to use brackets/parenthesis
  • iphone - Hide the annotation view in map view
  • Objective C IBOutlets
  • Why does Sencha 2 only work in Webkit browsers?
  • How to add the custom button on google's(device) native application in android?
  • in batch how do i use taskkill properly
  • change search magnifying glass to UIActivityIndicatorView
  • Blackberry 6: how to detect a long click on track pad?
  • Doctrine/Symfony entity generator and generating entity from one table
  • Clear activity stack before launching another activity
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Force show.bind execution
  • Refering to the class itself from within a class mehod in Objective C
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Android screen density dpi vs ppi
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • How would I use PHP exceptions to define a redirect?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to extract text from Word files using C#?
  • Do create extension work in single-user mode in postgres?
  • Matrix multiplication with MKL
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can I remove ASP.NET Designer.cs files?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal