C 检查或转换指数
我的指数格式为:“1.45e004”或“1.45e-04”或“-1.45e004”。
[请注意第三个中的减号]
检查 (==、!=、>、<、<=、>= 等) 不适用于其指数中的指数 [不返回结果或不返回结果]当前格式。
但是,使用 fabs 转换指数适用于前两个,但不适用于最后一个,因为 fabs 删除了减号(因此使值变为正值而不是负值)。
因此,我的问题是:
1) 有没有办法将指数转换为包含有符号值的绝对值?
[fabs 无法做到这一点]
或
2) 有没有一种方法 [或函数] 将一个指数与另一个指数进行比较(必须能够执行 ==、!=、>、< 等操作) 、>=、<=)?
或
3) 有没有办法从指数中提取两个值。
例如:“-1.45e-04”[值 1 = -1.45,值 2 = -4]。
问题 1 和/或 2 的答案是首选,因为 3 是一种解决方法,可能会出现实施问题(但是,如果 1 和 2 不这样做,它就存在)没有任何合适的答案)。
[旁注:可以使用 C++ 方法,但最好避免使用流]
谢谢
int main(void)
{
//Not the actual program, but simple enough
char Arr[100];
double T1, T2;
sprintf(Arr,"-1.45e004");
T1 = atof(Arr);
printf("%f\n",fabs(T1)); //Fails to show the minus sign
return 0;
}
总体目标可以实现,因此回答三个问题中的任何一个:
要么: 1) 直接比较(使用函数)给定格式的两个指数,看看它们是否相同、不同、大于、小于(等),以对最大指数到最小指数进行排序(不包含在程序如何实现排序并不相关:只需要解决检查问题)。
2)间接比较,通过将其转换为另一种可以直接比较的类型。
3)直接比较,使用一种尴尬的解决方法,即分别比较指数中的两个数字。
[背景详细信息]
实际程序跨越 7 个头文件和 1 个 cpp,因此我无法显示特定的段,因为它是交织在一起的。
它的任务是:从 ACE 卫星下载的文件被解析为参数,加载到内存中,转换成适当的类型。然后对存储的类型进行排序并准备(当前阶段)以呈现图形(尚未实现)。
目前,我正在处理指数(以上面给出的格式存储在 ACE 卫星文本文件中)。需要扫描这些指数以找到最大指数和最小指数,以便图表可以在两者之间正确间隔。之后,将比较每个指数,看看它是否大于(图表上较高)或小于(图表上较低)给定的一组数字。
对于该任务,我需要指数之间的比较。鉴于我对他们缺乏经验,我决定在这里询问。
I have exponents in the represented format of: "1.45e004" or "1.45e-04" or "-1.45e004".
[please note the minus sign in the third one]
Checking (==, !=, >, <, <=, >=, etc) does not work on exponents [incorrect or no results are returned] in their current format.
However, converting an exponent using fabs works for the first two, but not the last one, as fabs removes the minus sign (ergo making the value positive and not negative as it should be).
My questions are thus:
1) Is there a way to convert exponents into absolutes which would include signed values?
[fabs cannot do this]
OR
2) Is there a way [or function(s)] to compare one exponent to another (must be able to do ==, !=, >, <, >=, <=)?
OR
3) Is there a way to extract the two values from the exponent.
EG: "-1.45e-04" [Value1 = -1.45, Value2 = -4].
Answers for questions 1 and/or 2 are greatly preferred as 3 is a work around that may have issues with implementation (however, it's there in-case 1 and 2 don't have any suitable answers).
[Side-note: C++ methods can be used, although avoidance of stream is preferred ]
Thank you
int main(void)
{
//Not the actual program, but simple enough
char Arr[100];
double T1, T2;
sprintf(Arr,"-1.45e004");
T1 = atof(Arr);
printf("%f\n",fabs(T1)); //Fails to show the minus sign
return 0;
}
The overall goal can be achieved thus having any of the three questions answered:
Either:
1) Direct comparison (using a function) between the two exponents in the given format to see if they are the same, not the same, greater, less than (etc) for sorting of the greatest exponent to the least exponent (not contained in the program as how the sorting is implement is not relevant: only the checking needs to be resolved).
2) Indirect comparison, by converting it into another type that can be directly compared.
3) Direct comparison using an awkward work around by comparing the two numbers in the exponent separately.
[Background details]
The actual program spans 7 header files and 1 cpp, so I cannot display a particular segment given it's intertwined.
It task is thus: files downloaded from the ACE satellite are parsed as arguments, loaded into memory, converted into their appropriate types. The stored types are then sorted and prepared (current stage) to render a graph (not yet implemented).
At the moment, I am dealing with exponents (stored in the ACE satellite text files in the format given above). These exponents need to be scanned through to find the greatest exponent and the least exponent, so the graph can be correctly spaced between the two. After this, each exponent will be compared to see if it is greater (higher on the graph) or lesser than (lower on the graph) a given set of numbers.
For that task, I need comparisons between exponents. Given I'm inexperienced with them, I decided to ask here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
目前尚不清楚为什么您在这一行中调用 fabs() :
这就是 fabs() 的作用,它使负数变为正数。也许你想要这样:
可以直接比较两个浮点数:
It's unclear why your are calling
fabs()
in this line:That's what
fabs()
does, it makes negative numbers positive. Perhaps you want this:Comparison of two floating point numbers can be done directly:
看来您需要阅读每个计算机科学家应该了解什么浮点算术,至少可以帮助您掌握词汇。
也许您可以使用以下方法计算指数:
您的问题很难理解,没有明确定义“检查”浮点数的含义,您想要确定的是什么?
更新:要比较数字,就去做吧。所有三个样本值都是合法的浮点数,可以使用标准内置运算符直接进行比较。
It seems you need to read What Every Computer Scientist Should Know About Floating-Point Arithmetic, to at least help you with the vocabulary.
Perhaps you can compute the exponent using:
Your question is hard to understand, it's not well-defined what it means to "check" a floating point number, what it is that you are trying to determine?
UPDATE: To compare numbers, just do it. All of your three sample values are just legal floating-point numbers and can be directly compared using the standard build-in operators.
我认为您想要做的是从以科学计数法表示的数字中提取系数和基数部分。没有标准库函数可以执行此操作,但您可以轻松编写一个:
用法:
测试:
I think what you want to do is to extract the coefficient and base parts from a number expressed in scientific notation. There is no standard library function to do this, but you can write one easily:
Usage:
Tests:
您应该在此示例中找到您要查找的内容:
You should find what you're looking for in this example: