Hello,
I want to efficient convert floating point numbers (IEEE754) into a
string. I have no library routines that do the job (like sprintf etc.),
because I work in an embedded environment.
My actual algorithm uses multiplying with 10 to shift the fraction into
an integer value and to aquire the used exponent. But the drawback is
obvious: When I have very small numbers like 3.141E-300 I have to make
300 time consuming floating point multiplies to convert this number.
But, since I know the IEEE754 structure and have directly access to the
exponent (of base 2) of a fp number, is there a faster method to convert
fp numbers to ASCII?
Regards
Peter 14 7972
Peter Sprenger wrote:
Hello,
I want to efficient convert floating point numbers (IEEE754) into a
string. I have no library routines that do the job (like sprintf etc.),
because I work in an embedded environment.
My actual algorithm uses multiplying with 10 to shift the fraction into
an integer value and to aquire the used exponent. But the drawback is
obvious: When I have very small numbers like 3.141E-300 I have to make
300 time consuming floating point multiplies to convert this number.
But, since I know the IEEE754 structure and have directly access to the
exponent (of base 2) of a fp number, is there a faster method to convert
fp numbers to ASCII?
If you have the frexp() function available, that might
make a good starting point.
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman wrote:
Peter Sprenger wrote:
>Hello,
I want to efficient convert floating point numbers (IEEE754) into a string. I have no library routines that do the job (like sprintf etc.), because I work in an embedded environment.
My actual algorithm uses multiplying with 10 to shift the fraction into an integer value and to aquire the used exponent. But the drawback is obvious: When I have very small numbers like 3.141E-300 I have to make 300 time consuming floating point multiplies to convert this number.
But, since I know the IEEE754 structure and have directly access to the exponent (of base 2) of a fp number, is there a faster method to convert fp numbers to ASCII?
If you have the frexp() function available, that might
make a good starting point.
Nope, no frexp() available either.
Peter Sprenger wrote:
Eric Sosman wrote:
>Peter Sprenger wrote:
>>Hello,
I want to efficient convert floating point numbers (IEEE754) into a string. I have no library routines that do the job (like sprintf etc.), because I work in an embedded environment.
My actual algorithm uses multiplying with 10 to shift the fraction into an integer value and to aquire the used exponent. But the drawback is obvious: When I have very small numbers like 3.141E-300 I have to make 300 time consuming floating point multiplies to convert this number.
But, since I know the IEEE754 structure and have directly access to the exponent (of base 2) of a fp number, is there a faster method to convert fp numbers to ASCII?
If you have the frexp() function available, that might make a good starting point.
Nope, no frexp() available either.
I'd suggest writing your own frexp() work-alike, using
your knowledge of the floating-point representation. The
advantage of using Standard-like tools for the task is that
you'll easily be able to move the code to other platforms;
something like a float-to-string-without-sprintf operation
seems of sufficiently wide applicability that you may well
want it again. So: invest a little non-portable work to get
yourself up to the frexp()-ish baseline, and write portable C
from there upwards.
Good luck!
--
Eric Sosman es*****@acm-dot-org.invalid
>
I'd suggest writing your own frexp() work-alike, using
your knowledge of the floating-point representation. The
advantage of using Standard-like tools for the task is that
you'll easily be able to move the code to other platforms;
something like a float-to-string-without-sprintf operation
seems of sufficiently wide applicability that you may well
want it again. So: invest a little non-portable work to get
yourself up to the frexp()-ish baseline, and write portable C
from there upwards.
Good luck!
I was a little bit quick. In fact I can write a frexp() myself, that
separates exponent and mantissa. But then? I have a mantissa and an
exponent of base 2. I have no right idea to transform it from here to
an ascii string.
Regards
Peter
On Thu, 24 May 2007 14:44:39 +0200, Peter Sprenger wrote:
Hello,
I want to efficient convert floating point numbers (IEEE754) into a
string. I have no library routines that do the job (like sprintf etc.),
because I work in an embedded environment.
My actual algorithm uses multiplying with 10 to shift the fraction into
an integer value and to aquire the used exponent. But the drawback is
obvious: When I have very small numbers like 3.141E-300 I have to make
300 time consuming floating point multiplies to convert this number.
But, since I know the IEEE754 structure and have directly access to the
exponent (of base 2) of a fp number, is there a faster method to convert
fp numbers to ASCII?
Regards
Peter
If you have log, pow and floor then given a positive x
double log10 = log(x)/log(10.0);
int f = floor( log10);
double y = x*pow( 10.0, -f);
gets you y with 1<=y<10 and x = y * pow(10,f);
Duncan
On Thu, 24 May 2007 16:46:06 +0200, Peter Sprenger wrote:
>> I'd suggest writing your own frexp() work-alike, using your knowledge of the floating-point representation. The advantage of using Standard-like tools for the task is that you'll easily be able to move the code to other platforms; something like a float-to-string-without-sprintf operation seems of sufficiently wide applicability that you may well want it again. So: invest a little non-portable work to get yourself up to the frexp()-ish baseline, and write portable C from there upwards.
Good luck!
I was a little bit quick. In fact I can write a frexp() myself, that
separates exponent and mantissa. But then? I have a mantissa and an
exponent of base 2. I have no right idea to transform it from here to
an ascii string.
Regards
Peter
If x is positive
int expon;
double frac = frexp(x, &expon);
int f = floor( expon*log_10_2) ;
double y = x*pow( 10.0, -f);
gets you y with 1<=y<10 and x = y*pow(10,f)
here log_10_2 is log base 10 of 2, ie around 0.3010299956639 81143
You can get the digits of f by eg iterating
d = (int)y; y = 10.0*(y-d);
Note that you're only doing this for the number of digits required.
If you don't have pow (and maybe even if you do) it can be written
fairly easily & efficiently since its second argument is an integer.
Duncan
On Thu, 24 May 2007 16:57:28 +0100, Duncan Muirhead wrote:
On Thu, 24 May 2007 16:46:06 +0200, Peter Sprenger wrote:
>>> I'd suggest writing your own frexp() work-alike, using your knowledge of the floating-point representation. The advantage of using Standard-like tools for the task is that you'll easily be able to move the code to other platforms; something like a float-to-string-without-sprintf operation seems of sufficiently wide applicability that you may well want it again. So: invest a little non-portable work to get yourself up to the frexp()-ish baseline, and write portable C from there upwards.
Good luck! I was a little bit quick. In fact I can write a frexp() myself, that separates exponent and mantissa. But then? I have a mantissa and an exponent of base 2. I have no right idea to transform it from here to an ascii string.
Regards
Peter
If x is positive
int expon;
double frac = frexp(x, &expon);
int f = floor( expon*log_10_2) ;
double y = x*pow( 10.0, -f);
gets you y with 1<=y<10 and x = y*pow(10,f)
here log_10_2 is log base 10 of 2, ie around 0.3010299956639 81143
You can get the digits of f by eg iterating
d = (int)y; y = 10.0*(y-d);
Note that you're only doing this for the number of digits required.
If you don't have pow (and maybe even if you do) it can be written
fairly easily & efficiently since its second argument is an integer.
Duncan
Oops! Sorry, that's not quite right. In fact f above could be one
too small, and so we could have 10<=y<=100. I think the easiest thing
too do is to check for y being at least10, and if so fix it up.
Duncan
Peter Sprenger wrote On 05/24/07 10:46,:
> I'd suggest writing your own frexp() work-alike, using your knowledge of the floating-point representation. The advantage of using Standard-like tools for the task is that you'll easily be able to move the code to other platforms; something like a float-to-string-without-sprintf operation seems of sufficiently wide applicability that you may well want it again. So: invest a little non-portable work to get yourself up to the frexp()-ish baseline, and write portable C from there upwards.
Good luck!
I was a little bit quick. In fact I can write a frexp() myself, that
separates exponent and mantissa. But then? I have a mantissa and an
exponent of base 2. I have no right idea to transform it from here to
an ascii string.
You wrote originally about the time eaten up by the
very many multiplications by ten needed to scale a value
like 3.141E-300 to a reasonable range. I'm suggesting
that you use the exponent of two to figure out how many
"decades" of scaling you need, and do them all in one
multiplication. You could use a precomputed array with
the exponents as indices, or multiply the two's exponent
by log10(2) and do a little rounding and/or truncating
to get the ten's exponent.
x = m * 2**e
= m * (10**log10(2))* *e
= m * 10**(log10(2)*e )
= m * 10**f
= m * 10**floor(f) * 10**(f - floor(f))
= (m * 10**(f - floor(f))) * 10**floor(f)
-- Er*********@sun .com
>
You wrote originally about the time eaten up by the
very many multiplications by ten needed to scale a value
like 3.141E-300 to a reasonable range. I'm suggesting
that you use the exponent of two to figure out how many
"decades" of scaling you need, and do them all in one
multiplication. You could use a precomputed array with
the exponents as indices, or multiply the two's exponent
by log10(2) and do a little rounding and/or truncating
to get the ten's exponent.
x = m * 2**e
= m * (10**log10(2))* *e
= m * 10**(log10(2)*e )
= m * 10**f
= m * 10**floor(f) * 10**(f - floor(f))
= (m * 10**(f - floor(f))) * 10**floor(f)
Hello Eric,
you are right,
x = m * 2**e
is correct. But if I have use log10 in some way, isn't it easier to
directly get the exponent with directly log10(f) ? (f is my fp number)
And then multiply f to get it in the range 1.0 <= f < 10.0 ?
Your solution will not bring the answer on how many decimal places in
the fraction f has. So I have to multiply it anyway in turns to get the
decimal places after the comma.
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roger Leigh |
last post by:
Hello,
I'm writing a fixed-precision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a long int to store the
value, and the precision (number of decimal points) is variable (it's
a templated class):
template <size_t _decimal_places = 4>
class FixedFloat {
|
by: JS |
last post by:
We have the same floating point intensive C++ program that runs on
Windows on Intel chip and on Sun Solaris on SPARC chips. The program
reads the exactly the same input files on the two platforms. However,
they generate slightly different results for floating point numbers.
Are they really supposed to generate exactly the same results? I
guess so because both platforms are supposed to be IEEE floating point
standard (754?) compliant. ...
|
by: BigMan |
last post by:
How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
|
by: 63q2o4i02 |
last post by:
Hi,
I'm using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary. In either case, since I'm using PyVisa,
it converts the number to a single precision floating point, and that's
what I have to work with.
The question is how do I recover the bits out of this...
|
by: riya1012 |
last post by:
hello guys,
I need some help from you. I am doing a DSP project and for that I need
to do some C coding for the conversion of sample data which is in
floating point representation to fixed point representation.
the sample data is in floating point like
0.224128
2.299965
0.448350
-1.779926
| |
by: Robert Gamble |
last post by:
9899:1999 5.1.2.3 Example 4 reads:
"EXAMPLE 4 Implementations employing wide registers have to take care
to honor appropriate semantics. Values are independent of whether they
are represented in a register or in memory. For example, an implicit
spilling of a register is not permitted to alter the value. Also, an
explicit store and load is required to round to the precision of the
storage type. In particular, casts and assignments are...
|
by: rembremading |
last post by:
Hi all!
The following piece of code has (for me) completely unexpected behaviour.
(I compile it with gcc-Version 4.0.3)
Something goes wrong with the integer to float conversion.
Maybe somebody out there understands what happens.
Essentially, when I subtract the (double) function value GRID_POINT(2) from
a variable which has been assigned the same value before this gives a
non-zero result and I really do not understand why.
|
by: arnuld |
last post by:
Next month I will start to work on a C++ based Software named CAT++ which
is going to provide FORTRAN like arrays in C++ and will be used within
Scientific Community and hence will heavily depend on
Numerical-Computations. I was reading 29.16 ans 29.17 sections of FAQs:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16
as a test, I just tried this program on Linux, GCC 4.2.2 and it gave me 2
different results:
|
by: ma740988 |
last post by:
Consider the equation (flight dynamics stuff):
Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) /
3.1415926535897932384626433832795 (Radians)
There's a valid reason to use single precision floating point types.
The number of decimal digits guaranteed to be correct on my
implementation is 6. (i.e numeric_limits < float >::digits10 = 6 )
If I'm reading the IEEE standard, I'd could paraphrase the issue
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |