• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strange issue when converting from hex to float
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange issue when converting from hex to float


  • Subject: Re: Strange issue when converting from hex to float
  • From: Sherm Pendley <email@hidden>
  • Date: Fri, 22 Jul 2005 12:20:04 -0400

On Jul 22, 2005, at 11:36 AM, Ian was here wrote:

Here's a copy of the offending method...thanks for
offering to take a look.

This was explained several times now - didn't you get *any* of the responses?


Once again for the record:

long a = 5;
float b = a/255L;

Assignment (=) has a lower precedence than division (/), so the division is performed first. Both 5 and 255L are longs, so no conversion is necessary and an integer division is performed, with a result of 0.

After the division is complete, the assignment takes place; at that point a conversion is necessary to convert the integer result on the right-hand side of the assignment so it can be stored in the float variable on the left-hand side.

What you need to do is force the division to be done with floats; the easiest way is to use a float constant:

float b = a/255.0;

You could also typecast one or both of the arguments to the division:

float b = (float)a/255L;

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Strange issue when converting from hex to float
      • From: Ian was here <email@hidden>
References: 
 >Re: Strange issue when converting from hex to float (From: Ian was here <email@hidden>)

  • Prev by Date: PackageMaker
  • Next by Date: Re: Starting out with adding Applescript support to my Cocoa app (basic question about the best script interface)
  • Previous by thread: Re: Strange issue when converting from hex to float
  • Next by thread: Re: Strange issue when converting from hex to float
  • Index(es):
    • Date
    • Thread