All of lore.kernel.org
 help / color / mirror / Atom feed
From: tanure@linux.com (Lucas Tanure)
To: kernelnewbies@lists.kernelnewbies.org
Subject: IEEE-754 Float to int
Date: Wed, 23 Nov 2016 12:30:09 +0000	[thread overview]
Message-ID: <CAJX_Q+1__z6P8g2Jg5YDaPv1y8AQXRzYTFErzCOEeTVeJTnLag@mail.gmail.com> (raw)
In-Reply-To: <20161122165550.GA4925@kroah.com>

On Tue, Nov 22, 2016 at 4:55 PM, Greg KH <greg@kroah.com> wrote:
> On Tue, Nov 22, 2016 at 04:44:43PM +0000, Lucas Tanure wrote:
>>
>>
>> On Tue, Nov 22, 2016 at 4:39 PM, Greg KH <greg@kroah.com> wrote:
>>
>>     On Tue, Nov 22, 2016 at 04:05:18PM +0000, Lucas Tanure wrote:
>>     > Hi,
>>     >
>>     > At some point my hardware gives me a 32bit IEEE-754 float, like this :
>>     >
>>     >
>>     > regmap_read(device->regmap, ADDR0, &temp);
>>     >
>>     > value = temp << 16;
>>     >
>>     > regmap_read(device->regmap, ADDR1, &temp);
>>     >
>>     > value |= temp;
>>     >
>>     >
>>     > So, value has a 32bit float now, and I would like to print just the
>>     integer
>>     > part, like :
>>     > Read 26.92387 --> Print 26.
>>     > Simple, no float operations.
>>     >
>>     > How I can do it ?
>>
>>     Just print the upper 16 bits shifted right by 16 bits.
>>
>>
>>     But why would you want to print the value anyway?  Who would use it?
>>
>> My hardware gives me the board temperature as a float 32bits.
>> And the hwmon sysfs api asks me to return the temperature as millidegree
>> Celsius.
>>
>> To follow the hwmon sysfs api rules, I need to get my float, multiply by 1000
>> and print the integer part.
>> There is a better way to do that ?
>
> Ah, yeah, sorry, my example is wrong.  There's got to be other drivers
> that also do this same thing, try asking on the hwmon mailing list, they
> should know how to do this best, as they are the ones responsible for
> that user/kernel api.
>
> thanks,
>
> greg k-h

Hi,

For future reference:

The best way to do it seems to be to write conversion function.
Receive the float in a u32, parse it's bits and return a s32. Can be
done without the use of floats.

Thanks!

-- 
Lucas Tanure

  reply	other threads:[~2016-11-23 12:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-22 16:05 IEEE-754 Float to int Lucas Tanure
2016-11-22 16:19 ` Anupam Kapoor
2016-11-22 16:21   ` Lucas Tanure
2016-11-22 16:28     ` Anupam Kapoor
2016-11-22 16:30       ` Lucas Tanure
2016-11-22 16:39 ` Greg KH
2016-11-22 16:44   ` Lucas Tanure
2016-11-22 16:55     ` Greg KH
2016-11-23 12:30       ` Lucas Tanure [this message]
2016-11-23 14:04         ` Anupam Kapoor
     [not found] <CAJX_Q+1iPpLnbLYB6=9gWR2EEwek5VvioBCZB6gYsChOZ3EyVA@mail.gmail.com>
2016-11-22 17:10 ` Lucas Tanure
2016-11-22 20:33   ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJX_Q+1__z6P8g2Jg5YDaPv1y8AQXRzYTFErzCOEeTVeJTnLag@mail.gmail.com \
    --to=tanure@linux.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.