I am trying to use cffi-grovel on GSL's physical constants. For example, in http://git.savannah.gnu.org/cgit/gsl.git/tree/const/gsl_const_mksa.h, these are defined: #define GSL_CONST_MKSA_SPEED_OF_LIGHT (2.99792458e8) /* m / s */ #define GSL_CONST_MKSA_GRAVITATIONAL_CONSTANT (6.673e-11) /* m^3 / kg s^2 */
When I try to define these using CFFI-grovel (constant (+mksa-speed-of-light+ "GSL_CONST_MKSA_SPEED_OF_LIGHT")) I end up with an integer, because the C definition is cast to an integer prior to generating the constant.
Is there a way to get around this, so that I can grovel float constants?
Thanks, Liam
On Mon, 2009-05-25 at 17:21 -0400, Liam Healy wrote:
I am trying to use cffi-grovel on GSL's physical constants. For example, in http://git.savannah.gnu.org/cgit/gsl.git/tree/const/gsl_const_mksa.h, these are defined: #define GSL_CONST_MKSA_SPEED_OF_LIGHT (2.99792458e8) /* m / s */ #define GSL_CONST_MKSA_GRAVITATIONAL_CONSTANT (6.673e-11) /* m^3 / kg s^2 */
When I try to define these using CFFI-grovel (constant (+mksa-speed-of-light+ "GSL_CONST_MKSA_SPEED_OF_LIGHT")) I end up with an integer, because the C definition is cast to an integer prior to generating the constant.
Is there a way to get around this, so that I can grovel float constants?
not at the moment. you would need to modify the CONSTANT directive(add a :type key arg that defaults to :int). patches accepted, otherwise I'll try to do it myself this weekend or next week
On Mon, May 25, 2009 at 6:27 PM, Stelian Ionescu stelian.ionescu-zeus@poste.it wrote:
On Mon, 2009-05-25 at 17:21 -0400, Liam Healy wrote:
I am trying to use cffi-grovel on GSL's physical constants. For example, in http://git.savannah.gnu.org/cgit/gsl.git/tree/const/gsl_const_mksa.h, these are defined: #define GSL_CONST_MKSA_SPEED_OF_LIGHT (2.99792458e8) /* m / s */ #define GSL_CONST_MKSA_GRAVITATIONAL_CONSTANT (6.673e-11) /* m^3 / kg s^2 */
When I try to define these using CFFI-grovel (constant (+mksa-speed-of-light+ "GSL_CONST_MKSA_SPEED_OF_LIGHT")) I end up with an integer, because the C definition is cast to an integer prior to generating the constant.
Is there a way to get around this, so that I can grovel float constants?
not at the moment. you would need to modify the CONSTANT directive(add a :type key arg that defaults to :int). patches accepted, otherwise I'll try to do it myself this weekend or next week
My hack is attached. I supply a :format key arg instead of :type, less fuss in mapping types to format specifiers in C. This now works for
(constant (+mksa-speed-of-light+ "GSL_CONST_MKSA_SPEED_OF_LIGHT") :format "e")
which yields: +mksa-speed-of-light+ 2.997925e8 (type-of +mksa-speed-of-light+) DOUBLE-FLOAT
Because C uses "e" as the exponent character even for double floats, if you specify "e" for format, what you get will be read by Lisp is a single float. Therefore, I rebind *read-default-float-format* at the top of the file with an eval-when. The value is left as double-float which I'm not too happy about (though I always use double-float, I don't want to change the default). Since C historically used double for all floating types I think there's some justification in reading everything as a double-float, but there's probably a better way to do this.
Please improve on this.
Liam
Never mind my previous patch, this one is better.
Previously, in order to read double floats, I reset *read-default-float-format*, but that makes global changes, sometimes undesirable. So instead, in this version, if the format is specified as "D", it will create a number that Lisp will read as double float. Otherwise the :format argument should be NIL or a C format string ("D" is unused in C).
So for example (constant (+mksa-plancks-constant-h+ GSL_CONST_MKSA_PLANCKS_CONSTANT_H) :format "D") becomes (cl:defconstant +mksa-plancks-constant-h+ 6.626069d-34)
If the :format argument is not given or is nil, the expansion of constant should be identical to the original.
Liam
With unified diff.
Liam
On Thu, May 28, 2009 at 10:51 PM, Liam Healy lnp@healy.washington.dc.us wrote:
Never mind my previous patch, this one is better.
Previously, in order to read double floats, I reset *read-default-float-format*, but that makes global changes, sometimes undesirable. So instead, in this version, if the format is specified as "D", it will create a number that Lisp will read as double float. Otherwise the :format argument should be NIL or a C format string ("D" is unused in C).
So for example (constant (+mksa-plancks-constant-h+ GSL_CONST_MKSA_PLANCKS_CONSTANT_H) :format "D") becomes (cl:defconstant +mksa-plancks-constant-h+ 6.626069d-34)
If the :format argument is not given or is nil, the expansion of constant should be identical to the original.
Liam