Subject: |
Re: Conversion problem from double to 32 bit unsigned int (in ao) |
From: |
Benjamin Franksen <[email protected]> |
To: |
<[email protected]> |
Date: |
Fri, 12 Apr 2013 00:37:01 +0200 |
Am Donnerstag, 11. April 2013, 22:37:35 schrieb Andrew Johnson:
> On 2013-04-10 Dirk Zimoch wrote:
> > Today I found a strange problem. I have a device that has 32 bit
> > unsigned int output registers. I want to scale an analog value (e.g.
> > -10.0 .. +10.0) to the raw range 0x00000000 ... 0xffffffff. The
> > special_linconv() function of the ao record sets ESLO and EOFF correctly
> > (ESLO=20/4294967295=4.65661287416E-09, EOFF=-10.0). All negative values
> > work but any positive value gives wrong results. Even worse, the results
> > are different on different machines: 0x80000000 on linux-x86, 0x7fffffff
> > on vxworks-ppc604 for all positive values.
> >
> > What happens? In the convert() function the analog (double) value is
> > converted to epicsInt32 (type of the RVAL field). However if the double
> > value is too large for the integer, strange and obviously implementation
> > dependent things happen. I cannot find anything about this case in K&R.
>
> Benjamin quoted the wrong part of K&R §A6.3 because we're not dealing with
> unsigned types here at all.
Right, I was a bit too quick here. Sorry if this caused confusion.
> ...
> > There's a guy called John Reghr,
John Regehr.
> > a professor at the University of Utah, who
> writes about undefined behaviors in C and C++ code quite a lot; I recommend
> reading his stuff.
Many thanks for the pointer. Very interesting reading indeed.
> He ran an interesting competition recently to write a
> simple ascii-to-long numeric converter that must not exhibit UB, so no
> overflows allowed but it must detect numbers that won't fit into the long.
Some time ago I had to deal with a similar problem in the snc code. Even if
you can use standard libraries and must parse only unsigned values the
solution is less trivial than what one might think at first.
Cheers
--
Ben Franksen
() ascii ribbon campaign - against html e-mail
/\ www.asciiribbon.org - against proprietary attachments
________________________________
Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
Mitglied der Hermann von Helmholtz-Gemeinschaft Deutscher Forschungszentren e.V.
Aufsichtsrat: Vorsitzender Prof. Dr. Dr. h.c. mult. Joachim Treusch, stv. Vorsitzende Dr. Beatrix Vierkorn-Rudolph
Geschäftsführung: Prof. Dr. Anke Rita Kaysser-Pyzalla, Thomas Frederking
Sitz Berlin, AG Charlottenburg, 89 HRB 5583
Postadresse:
Hahn-Meitner-Platz 1
D-14109 Berlin
http://www.helmholtz-berlin.de
- References:
- Conversion problem from double to 32 bit unsigned int (in ao) Dirk Zimoch
- Re: Conversion problem from double to 32 bit unsigned int (in ao) Andrew Johnson
- Navigate by Date:
- Prev:
Re: Conversion problem from double to 32 bit unsigned int (in ao) Andrew Johnson
- Next:
RE: asynAddress, asynPortDriver maxAddr, and addr in setXXXParam Emmanuel Mayssat
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
<2013>
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
- Navigate by Thread:
- Prev:
Re: Conversion problem from double to 32 bit unsigned int (in ao) Andrew Johnson
- Next:
RE: linux file search from Asyn nick.rees
- Index:
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
<2013>
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|