M4 eval精度 [英] M4 eval precision

查看:181
本文介绍了M4 eval精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用M4宏来生成CSS文件。我愿意输入我的值在px和做简单的数学使用eval()得到结果在em。不幸的是我没有找到如何获得浮点数。

I'm trying to use M4 macros to generate css files. I'm willing to enter my values in px and do simple math using eval() to get results in em. Unfortunatly I didn't find how to get floats.

define(`FONTSIZE', `13')dnl
define(`LINEHEIGHT', `17')dnl
.content {padding : eval(LINEHEIGHT / FONTSIZE)em}
>>> m4 style.css.m4
>>> .content {padding : 1em}

有任何想法吗?

谢谢!

推荐答案

出于特定目的,表达式可能会变成类似

For your particular purpose the expression may become something like

eval(LINEHEIGHT/FONTSIZE).substr(eval(((LINEHEIGHT%FONTSIZE)*1000)/FONTSIZE + 1000),1)

(当然,使用满足精度要求的10的幂)
这是获取浮动的常见技巧点状结果。其他操作符更复杂,不是说像sin,cos,ln ...这样的函数。

(of course, use the power of 10 that meets your precision requirements) This is a common trick to obtain a floating point-like result from a division. Other operators are more complicated, not to say the functions like sin, cos, ln...

一般来说,原则上可以写一些浮点操作宏用字符串操作和整数运算符实现,但是我认为,当操作不像分区那样简单时,使用由syscmd()调用的外部程序(bc,gawk ...)更为普遍。

In general it would be possible in principle to write some floating point manipulation macros implemented with string manipulations and integer operators, but I think it's more pratical to use an external program (bc, gawk...) invoked by "syscmd()" when the operations are not as simple as a division.

这篇关于M4 eval精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆