CGFloat:舍入,舍入,绝对和32/64位精度 [英] CGFloat: round, floor, abs, and 32/64 bit precision

查看:73
本文介绍了CGFloat:舍入,舍入,绝对和32/64位精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:我该如何以无警告的方式编译32位和64位CGFloat的方式调用标准浮点代码?

TLDR: How do I call standard floating point code in a way that compiles both 32 and 64 bit CGFloats without warnings?

CGFloat定义为double或float,具体取决于编译器设置和平台.我正在尝试编写在两种情况下均能正常工作的代码,而不会产生很多警告.

CGFloat is defined as either double or float, depending on the compiler settings and platform. I'm trying to write code that works well in both situations, without generating a lot of warnings.

当我使用floor,abs,ceil和其他简单的浮点运算之类的函数时,会收到有关值被截断的警告.例如:

When I use functions like floor, abs, ceil, and other simple floating point operations, I get warnings about values being truncated. For example:

警告:隐式转换将64位值缩短为32位值

warning: implicit conversion shortens 64-bit value into a 32-bit value

我并不担心计算的准确性或准确性,因为我意识到我可以一直使用所有函数的双精度版本(floor而不是floorf等);但是,我必须容忍这些错误.

I'm not concerned about correctness or loss of precision in of calculations, as I realize that I could just use the double precision versions of all functions all of the time (floor instead of floorf, etc); however, I would have to tolerate these errors.

有没有一种方法可以干净地编写支持32位和64位浮点数的代码,而不必使用大量的#ifdef __ LP64 __或为所有标准浮点函数编写包装函数?

Is there a way to write code cleanly that supports both 32 bit and 64 bit floats without having to either use a lot of #ifdef __ LP64 __ 's, or write wrapper functions for all of the standard floating point functions?

推荐答案

您可以使用 tgmath.h .

You may use those functions from tgmath.h.

#include <tgmath.h>

...

double d = 1.5;
double e = floor(d);   // will choose the 64-bit version of 'floor'

float f = 1.5f;
float g = floor(f);    // will choose the 32-bit version of 'floorf'.

这篇关于CGFloat:舍入,舍入,绝对和32/64位精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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