转换双浮动而不无限 [英] Convert double to float without Infinity

查看:124
本文介绍了转换双浮动而不无限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换使用双老咋浮动myFloat =(浮点)myDouble

本浮动但有时确实会导致无限,这是不利于进一步的处理我在做。我只要它是在作为原始数(相对数量上我转换,必须保持所有的数字强度)的总体方向指向我很好与损失

This does however sometimes result in "Infinity", which is not good for the further processing I'm doing. I'm okay with loss as long as it is pointing in the general direction as the original number (the relative number 'strength' on all numbers I'm converting must be maintained).

我怎么能转换成浮动翻一番,避免无限

How can I convert float to double and avoid Infinity?

背景:

我在读从WAV /麦克风字节流,将其转换为浮动,将其转换为双击,运行它通过FFT计算(这是需要双击),现在我想回浮动(存储在32位图像容器中的数据)。

Background:
I'm reading a bytestream from wav/mic, converting it to float, converting it to double, running it through FFT calculation (this is what requires double), and now I want it back to float (storing the data in 32-bit image container).

推荐答案

因此,如果该值大于 float.MaxValue 时,你高兴吧仅仅是 float.MaxValue ?这将有效地夹的值。如果可以的话,这是相当容易:

So if the value is greater than float.MaxValue, are you happy for it to just be float.MaxValue? That will effectively "clip" the values. If that's okay, it's reasonably easy:

float result = (float) input;
if (float.IsPositiveInfinity(result))
{
    result = float.MaxValue;
} else if (float.IsNegativeInfinity(result))
{
    result = float.MinValue;
}

这篇关于转换双浮动而不无限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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