* IEEE-754 Float to int @ 2016-11-22 16:05 Lucas Tanure 2016-11-22 16:19 ` Anupam Kapoor 2016-11-22 16:39 ` Greg KH 0 siblings, 2 replies; 12+ messages in thread From: Lucas Tanure @ 2016-11-22 16:05 UTC (permalink / raw) To: kernelnewbies 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 ? Thank! -- Tanure -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/cfd32e55/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 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:39 ` Greg KH 1 sibling, 1 reply; 12+ messages in thread From: Anupam Kapoor @ 2016-11-22 16:19 UTC (permalink / raw) To: kernelnewbies On Tue, Nov 22, 2016 at 9:35 PM, Lucas Tanure <tanure@linux.com> wrote: > So, value has a 32bit float now, and I would like to print just the > integer part, like : > Read 26.92387 --> Print 26. > ?how about just typecasting to int ? so : int x = (int)(some-float-value) that should be it right ?? ?-- kind regards anupam? In the beginning was the lambda, and the lambda was with Emacs, and Emacs was the lambda. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/9d72dfc7/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:19 ` Anupam Kapoor @ 2016-11-22 16:21 ` Lucas Tanure 2016-11-22 16:28 ` Anupam Kapoor 0 siblings, 1 reply; 12+ messages in thread From: Lucas Tanure @ 2016-11-22 16:21 UTC (permalink / raw) To: kernelnewbies On Tue, Nov 22, 2016 at 4:19 PM, Anupam Kapoor <anupam.kapoor@gmail.com> wrote: > > On Tue, Nov 22, 2016 at 9:35 PM, Lucas Tanure <tanure@linux.com> wrote: > >> So, value has a 32bit float now, and I would like to print just the >> integer part, like : >> Read 26.92387 --> Print 26. >> > > ?how about just typecasting to int ? so : > > int x = (int)(some-float-value) > But this "some-float-value" must be declared as float ? Can I do that ? > > that should be it right ?? > > > ?-- > kind regards > anupam? > > > In the beginning was the lambda, and the lambda was with Emacs, and Emacs > was the lambda. > -- Lucas Tanure Embedded Developer +55 19 988176559 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/827e6e75/attachment-0001.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:21 ` Lucas Tanure @ 2016-11-22 16:28 ` Anupam Kapoor 2016-11-22 16:30 ` Lucas Tanure 0 siblings, 1 reply; 12+ messages in thread From: Anupam Kapoor @ 2016-11-22 16:28 UTC (permalink / raw) To: kernelnewbies On Tue, Nov 22, 2016 at 9:51 PM, Lucas Tanure <tanure@linux.com> wrote: > But this "some-float-value" must be declared as float ? Can I do that ? > ?in your example, isn't 'value' a float type? already ? ?-- kind regards anupam? In the beginning was the lambda, and the lambda was with Emacs, and Emacs was the lambda. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/cf32bc03/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:28 ` Anupam Kapoor @ 2016-11-22 16:30 ` Lucas Tanure 0 siblings, 0 replies; 12+ messages in thread From: Lucas Tanure @ 2016-11-22 16:30 UTC (permalink / raw) To: kernelnewbies No. Is a unsigned int. I'm trying to avoid floats in the kernel. On Tue, Nov 22, 2016 at 4:28 PM, Anupam Kapoor <anupam.kapoor@gmail.com> wrote: > > On Tue, Nov 22, 2016 at 9:51 PM, Lucas Tanure <tanure@linux.com> wrote: > >> But this "some-float-value" must be declared as float ? Can I do that ? >> > > ?in your example, isn't 'value' a float type? already ? > > ?-- > kind regards > anupam? > > > In the beginning was the lambda, and the lambda was with Emacs, and Emacs > was the lambda. > -- Lucas Tanure Embedded Developer +55 19 988176559 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/36b0bec1/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:05 IEEE-754 Float to int Lucas Tanure 2016-11-22 16:19 ` Anupam Kapoor @ 2016-11-22 16:39 ` Greg KH 2016-11-22 16:44 ` Lucas Tanure 1 sibling, 1 reply; 12+ messages in thread From: Greg KH @ 2016-11-22 16:39 UTC (permalink / raw) To: kernelnewbies 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? thanks, greg k-h ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:39 ` Greg KH @ 2016-11-22 16:44 ` Lucas Tanure 2016-11-22 16:55 ` Greg KH 0 siblings, 1 reply; 12+ messages in thread From: Lucas Tanure @ 2016-11-22 16:44 UTC (permalink / raw) To: kernelnewbies 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 <https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt>, I need to get my float, multiply by 1000 and print the integer part. There is a better way to do that ? > thanks, > > greg k-h > Thanks -- Lucas Tanure -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161122/113d0829/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:44 ` Lucas Tanure @ 2016-11-22 16:55 ` Greg KH 2016-11-23 12:30 ` Lucas Tanure 0 siblings, 1 reply; 12+ messages in thread From: Greg KH @ 2016-11-22 16:55 UTC (permalink / raw) To: kernelnewbies 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-22 16:55 ` Greg KH @ 2016-11-23 12:30 ` Lucas Tanure 2016-11-23 14:04 ` Anupam Kapoor 0 siblings, 1 reply; 12+ messages in thread From: Lucas Tanure @ 2016-11-23 12:30 UTC (permalink / raw) To: kernelnewbies 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* IEEE-754 Float to int 2016-11-23 12:30 ` Lucas Tanure @ 2016-11-23 14:04 ` Anupam Kapoor 0 siblings, 0 replies; 12+ messages in thread From: Anupam Kapoor @ 2016-11-23 14:04 UTC (permalink / raw) To: kernelnewbies On Wed, Nov 23, 2016 at 6:00 PM, Lucas Tanure <tanure@linux.com> wrote: > 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. > ?but this conversion-function might not be straight-forward :) ?it _may_ be possibly simplified depending on your precision requirements etc. etc. ?-- kind regards anupam? In the beginning was the lambda, and the lambda was with Emacs, and Emacs was the lambda. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20161123/72d2a3d9/attachment.html ^ permalink raw reply [flat|nested] 12+ messages in thread
[parent not found: <CAJX_Q+1iPpLnbLYB6=9gWR2EEwek5VvioBCZB6gYsChOZ3EyVA@mail.gmail.com>]
* Re: IEEE-754 Float to int [not found] <CAJX_Q+1iPpLnbLYB6=9gWR2EEwek5VvioBCZB6gYsChOZ3EyVA@mail.gmail.com> @ 2016-11-22 17:10 ` Lucas Tanure 2016-11-22 20:33 ` Guenter Roeck 0 siblings, 1 reply; 12+ messages in thread From: Lucas Tanure @ 2016-11-22 17:10 UTC (permalink / raw) To: linux-hwmon Hi, My hardware gives me the board temperature as a float 32bits. So, 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 over hwmon sysfs API. Like : temp Current temperature as reported by thermal zone (sensor). Unit: millidegree Celsius RO, Required Read 26.92387 --> Read 26923 at /sys/class/hwmon/hwmon0/temp0_input Simple, no float operations. How I can do it ? Thanks! -- Lucas Tanure ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: IEEE-754 Float to int 2016-11-22 17:10 ` Lucas Tanure @ 2016-11-22 20:33 ` Guenter Roeck 0 siblings, 0 replies; 12+ messages in thread From: Guenter Roeck @ 2016-11-22 20:33 UTC (permalink / raw) To: Lucas Tanure; +Cc: linux-hwmon On Tue, Nov 22, 2016 at 05:10:54PM +0000, Lucas Tanure wrote: > Hi, > > My hardware gives me the board temperature as a float 32bits. > So, 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 over hwmon > sysfs API. Like : > > temp > Current temperature as reported by thermal zone (sensor). > Unit: millidegree Celsius > RO, Required > > > Read 26.92387 --> Read 26923 at /sys/class/hwmon/hwmon0/temp0_input > Simple, no float operations. > > How I can do it ? > Only idea I have is to write a conversion function. Note that the attribute name should be temp1_input. Guenter ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2016-11-23 14:04 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 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
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.