您可以修饰一个十六进制常量,以便将其解释为浮点数吗?

发布于 2024-11-27 12:45:58 字数 821 浏览 0 评论 0原文

只是对某件事感到好奇。在将 HTML 颜色(其各个颜色分量通过 2 位十六进制值表示)转换为 0.0 到 1.0 之间的浮点数以便我们可以在 OpenGL 中使用它们时,我对某些事情感到好奇。我们必须将十六进制值除以 255 才能得到相应的 OpenGL 值,但我们不能简单地按原样使用十六进制值,因为这会产生整数除法。

现在我知道这些都可以解决这个问题(当然)...

float r = 0xFD / (float)0xFF; // Works because of the cast to float

float r = 0xFD / 255.0f;      // Works because of the explicit float 

float d = 0xFF;
float r = 0xFD / d;           // Works because 'd' is a float 

...但是我想知道是否有任何方法可以装饰十六进制值,以便将其解释为浮点数(就像您对 1.0 中的“f”所做的那样) f) 无需进行转换、计算或临时变量。

这些当然是行不通的...

float r = 0xFD / 0xFF;   // Integer division yields an integer, not float

float r = 0xFD / 0xFFf;  // Interprets 'f' as part of the hex value

同样,我没有试图找出如何实现我需要的结果,因为我的代码工作得很好。我只是想知道是否可以通过使用类似于“f”处理十进制值的方式装饰十六进制值来使代码更清晰,而不是使用上述三种有效的方法。

Just curious about something. While converting HTML colors (whose individual color components are represented via 2-digit hexadecimal values) to floats between 0.0 and 1.0 so we can use them with OpenGL, I became curious about something. We have to divide the hex values by 255 to give us their OpenGL counterparts, but we can't simply use the hex values as-is because that produces integer division.

Now I know these all work around this issue (of course)...

float r = 0xFD / (float)0xFF; // Works because of the cast to float

float r = 0xFD / 255.0f;      // Works because of the explicit float 

float d = 0xFF;
float r = 0xFD / d;           // Works because 'd' is a float 

...but I was wondering if there's any way to just decorate a hex value so it's interpreted as a float (like you do with the 'f' in 1.0f) without having to do casting, calculations or interim variables.

These of course don't work...

float r = 0xFD / 0xFF;   // Integer division yields an integer, not float

float r = 0xFD / 0xFFf;  // Interprets 'f' as part of the hex value

Again, not trying to find out how to achieve my needed results as my code works just fine. I'm just wondering if I can make the code cleaner via decorating the hex value with something similar to how 'f' works with decimal values instead of using the above-three methods that do work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧话新听 2024-12-04 12:45:58

在 c++0x 中,使用 用户定义的文字 可能可以实现这一点但我怀疑在 c++03 中是否有任何巧妙的方法可以做到这一点。

This would likely be possible in c++0x using user defined literals but I doubt there is any neat way to do it in c++03.

谁的新欢旧爱 2024-12-04 12:45:58

恐怕您必须使用预处理器定义才能获得最佳外观。并且您还需要进行转换来执行计算。

我能想到的最好的解决方案是

#define COMPONENT8(value) ((float)(value) / 255.0f))

浮点值不能指定为十六进制(整数)值。

I'm afraid you have to use preprocessor definition in order to achieve the best look. And you need also casting to perform your computations.

The best solution I can think is

#define COMPONENT8(value) ((float)(value) / 255.0f))

Floating point values cannot be specified as hexadecimal (integer) values.

时光无声 2024-12-04 12:45:58

我不知道有什么方法可以完全满足您的要求,但如果您想重复使用 0xFF,为什么不将其设置为常量呢?这仍然会保留打字,即:

const float divisor = 0xFF
float r = 0xFD / divisor

我认为这会解决您想要解决的问题,即使它的方式不完全相同。

I don't know of any way to do exactly what you're asking, but if you're looking to use 0xFF repeatedly, why not set it as a constant? That will still preserve typing, i.e:

const float divisor = 0xFF
float r = 0xFD / divisor

I think that'd solve the issue you're looking to solve, even if it's not exactly in the same way.

强辩 2024-12-04 12:45:58

一种方法是定义自己的模式来定义十六进制浮点数,0xff 定义十六进制整数,您可以创建一个宏,例如:

#define 0y (float) 0x

然后您只需编写

float r = 0xAF / 0yFF;

即可进行浮点除法。

虽然此解决方案可能会给阅读您代码的其他人造成混乱,但如果代码主要适合您,那么您可以这样做

One way you could do it is by defining your own pattern to define hex floats, 0xff defines hex integers, you could make a macro, like:

#define 0y (float) 0x

Then you just write

float r = 0xAF / 0yFF;

to do your float division.

This solution could though create confusion for others who reads your code, but if the code is mostly for you, than you could do it this way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文