从数字中提取数字 [英] Extracting Digits from a Number

查看:82
本文介绍了从数字中提取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我知道一种使用%10和

除以10从数字中提取数字的方法。但我想知道是否那里有一个算法

不使用除以10的功能。


我问的原因是我在RISC系统上编程

除法相当昂贵,我希望能够从整数中提取

位数(整数长度为1到6位,

我知道这个数字有多少位数。


有什么想法吗?

Hello,

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?

推荐答案

在文章< 11 ********************** @ g47g2000cwa.googlegroups .com> ;,

Kosio < AA ******** @ gmail.com>写道:
In article <11**********************@g47g2000cwa.googlegroups .com>,
Kosio <aa********@gmail.com> wrote:
我知道一种使用%10和
除以10从数字中提取数字的方法。但我想知道是否有算法在那里
不使用除以10的特征。
我问的原因是我在RISC系统上进行编程,其中
除法非常昂贵,我希望能够从整数中提取
数字(整数将来自长度为1到6位,
我知道数字中有多少位数。
有什么想法吗?
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature. The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number). Any ideas?




分支表,查找表,重复减法,手动

除以10二元(移位,减法,乘法)

旧帖子
http://groups.google.ca/group/comp.o...be552f2742b678

引用

用常数除数优化整数除法作者:Robert Grappel

,显然是在Dr. Dobbs ......可能是15年前左右。

-

"这是一个黄金时代,一个冒险的高潮,丰富的生活和坚强的死亡......但是没有人这么认为。 - Alfred Bester,TSMD



branch tables, look-up tables, repeated subtraction, "manual"
division by 10 in binary (shifts, subtractions, multiplications)
An old posting
http://groups.google.ca/group/comp.o...be552f2742b678
references
"Optimizing Integer Division by a Constant Divisor" by Robert Grappel
which was apparently in Dr. Dobbs... probably 15 years ago or so.
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD


Kosio写道:
你好,

我知道一种提取数字的方法从一个数字使用%10和
除以10.但我想知道是否有一个算法在那里
不使用除以10的特征。

我问的原因是我在RISC系统上进行编程,其中
除法非常昂贵,我希望能够从整数中提取
数字(整数长度为1到6位) ,
我知道这个数字中有多少位数。

任何想法?
Hello,

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?




因为这是一个算法问题而不是C。一,你可能会在comp.programming中得到更多的意见。您可以从

表中获得包含1000000个条目的数字。或者执行/ 1000和%1000并从具有1000个条目的

表中获取它们。既然你关心效率,我认为使用sprintf的
就是正确的。我确定还有其他方法......


-David



As this is an algorithm question, not a "C" one, you might get
more opinions in comp.programming. You could get digits from a
table with 1000000 entries. Or do /1000 and %1000 and get them from a
table with 1000 entries. Since you care about efficiency, I assume
using sprintf is right out. I''m sure there are other ways...

-David


文章< 11 ***********************g47g2000cwa.googlegroups .com> ;,

Kosio< aa ******** @ gmail.com>写道:
In article <11**********************@g47g2000cwa.googlegroups .com>,
Kosio <aa********@gmail.com> wrote:

我知道一种使用%10和
除以10从数字中提取数字的方法。但我想知道是否有算法<不使用除以10的功能。

我问的原因是我在RISC系统上进行编程,其中
划分相当昂贵,我希望能够从整数中提取
数字(整数长度为1到6位,
我知道数字中有多少位数)。

任何想法?

I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.

The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).

Any ideas?




以下是一些想法,没有特别的顺序:


1.可能有一些编译器选项可以帮助你。很多

的RISC编译器(包括gcc)默认为给定处理器最广泛的与b $ b兼容的指令集。如果您愿意取消支持较旧/较便宜版本的CPU,那么

您可以利用硬件部门。


8.另一种方法是发布一个函数的两个版本,一个为硬件部门编译的
,另一个是w / o。您可以在安装时选择
,也可以在运行时选择最佳效果。


2.您可以在输入中利用一些冗余
数据。特殊情况-100到+100左右的数组,和/或

缓存该范围之外的最后几个数字。你不需要加速所有数字,只需要实际遇到的大部分




5.不要将数字存储为数字。把它们放在字节中

或nybbles,当你需要对它们进行算术运算时,

转换为数字只需要乘法并加,

不是除法,所以它应该更快。


4.取决于CPU的能力和奉献精神

谁在你的实施stdlib.h平台,理论上可能是
div()比执行

单独的%和/操作更快。



Here are some thoughts, in no particular order:

1. There may be some compiler options to help you out. A lot
of RISC compilers (incl. gcc) default to the mostly widely
compatible instruction set for a given processor. If you are
willing to de-support older/cheaper versions of a CPU then
you may be able to take advantage of division in hardware.

8. An alternative is to ship two versions of a function, one
compiled for hardware division and one w/o. You can either
select at install time or run time whatever works best.

2. You may be able to exploit some redundancy in the input
data. Special-case -100 thru +100 or so in an array, and/or
cache the last several numbers outside that range. You
don''t have to speed it up for all numbers, just most of the
ones actually encountered.

5. Don''t store the numbers as numbers. Put them in bytes
or nybbles and when you need to do arithmetic on them,
the conversion to numbers will only require multiply and add,
not division, so it should be faster.

4. Depending on the abilities of the CPU and the dedication
of whoever implented stdlib.h on your platform, it is
theoretically possible that div() is faster than doing
separate % and / operations.


这篇关于从数字中提取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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