COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Most significant digit and formatting floating point output

• Follow

```Dear all,
well, I'm tangled up with something quite stupid, but I don't want to reinv=
ent the wheel (for the 10th time today...).
Here are two versions of the same question:
1) Short version: given a number such as 0.003456789 how do I get the most =
significant digit position? I need this number to produce formatted output =
that would convert the number to simply 0.003. In general I can figure this=
out with some IFs and stuff like that, but I'm hoping there is a more "ele=
gant" way for this.

2) Long version: I'm analyzing some images and I get results with errors. T=
hese numbers are floating point and I would like to format the output so th=
at it looks something like this: 'My results are (0.123 +/- 0.003) units' a=
nd the source data is:
Result =3D 0.123456789
Error =3D 0.003456789
The point is that I need to find the first non-zero element in Error. Then =
I would use this number in the FORMAT parameter like this: MyFormat =3D '(f=
0'+STRTRIM(FirstNonZeroElement+2,2)+'.'+STRTRIM(FirstNonZeroElement,2)+')'
and use "MyFormat" as formatting for the string conversion of both.
(in the above example FirstNonZeroElement should be 3 and MyFormat would be=
'(f05.3)'.

Thanks in advance for any help.

Cheers,
Helder
```
 0
Reply helder5178 (48) 8/7/2012 1:33:13 PM

```Helder writes:

> well, I'm tangled up with something quite stupid, but I don't want to reinvent the wheel (for the 10th time today...).
> Here are two versions of the same question:
> 1) Short version: given a number such as 0.003456789 how do I get the most significant digit position? I need this number to produce formatted output that would convert the number to simply 0.003. In general I can figure this out with some IFs and stuff like that, but I'm hoping there is a more "elegant" way for this.
>
> 2) Long version: I'm analyzing some images and I get results with errors. These numbers are floating point and I would like to format the output so that it looks something like this: 'My results are (0.123 +/- 0.003) units' and the source data is:
> Result = 0.123456789
> Error = 0.003456789
> The point is that I need to find the first non-zero element in Error. Then I would use this number in the FORMAT parameter like this: MyFormat = '(f0'+STRTRIM(FirstNonZeroElement+2,2)+'.'+STRTRIM(FirstNonZeroElement,2)+')'
> and use "MyFormat" as formatting for the string conversion of both.
> (in the above example FirstNonZeroElement should be 3 and MyFormat would be '(f05.3)'.

You might be interested in the Number_Formatter program
in the Coyote Library:

http://www.idlcoyote.com/programs/number_formatter.pro

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
```
 0
Reply news8567 (866) 8/7/2012 1:44:05 PM

```On Tuesday, August 7, 2012 3:44:05 PM UTC+2, David Fanning wrote:
> Helder writes:=20
>=20
>=20
>=20
> > well, I'm tangled up with something quite stupid, but I don't want to r=
einvent the wheel (for the 10th time today...).
>=20
> > Here are two versions of the same question:
>=20
> > 1) Short version: given a number such as 0.003456789 how do I get the m=
ost significant digit position? I need this number to produce formatted out=
put that would convert the number to simply 0.003. In general I can figure =
this out with some IFs and stuff like that, but I'm hoping there is a more =
"elegant" way for this.
>=20
> >=20
>=20
> > 2) Long version: I'm analyzing some images and I get results with error=
s. These numbers are floating point and I would like to format the output s=
o that it looks something like this: 'My results are (0.123 +/- 0.003) unit=
s' and the source data is:
>=20
> > Result =3D 0.123456789
>=20
> > Error =3D 0.003456789
>=20
> > The point is that I need to find the first non-zero element in Error. T=
hen I would use this number in the FORMAT parameter like this: MyFormat =3D=
'(f0'+STRTRIM(FirstNonZeroElement+2,2)+'.'+STRTRIM(FirstNonZeroElement,2)+=
')'
>=20
> > and use "MyFormat" as formatting for the string conversion of both.
>=20
> > (in the above example FirstNonZeroElement should be 3 and MyFormat woul=
d be '(f05.3)'.
>=20
>=20
>=20
> You might be interested in the Number_Formatter program
>=20
> in the Coyote Library:
>=20
>=20
>=20
>    http://www.idlcoyote.com/programs/number_formatter.pro
>=20
>=20
>=20
> Cheers,
>=20
>=20
>=20
> David
>=20
>=20
>=20
>=20
>=20
>=20
>=20
> --=20
>=20
> David Fanning, Ph.D.
>=20
> Fanning Software Consulting, Inc.
>=20
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
>=20
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")

Thanks David,
If I understand number_formatter correctly, it accepts a number of decimals=
as an input, but it does not determine which one is the first significant =
digit.

I'm starting to think that I'll have to code this as in your program and lo=
ok for the first digit that is not a zero or a dot.

Thanks,
Helder
```
 0
Reply helder5178 (48) 8/7/2012 2:09:45 PM

```On Tuesday, August 7, 2012 9:33:13 AM UTC-4, Helder wrote:
> Dear all,
>=20
> well, I'm tangled up with something quite stupid, but I don't want to rei=
nvent the wheel (for the 10th time today...).
>=20
> Here are two versions of the same question:
>=20
> 1) Short version: given a number such as 0.003456789 how do I get the mos=
t significant digit position? I need this number to produce formatted outpu=
t that would convert the number to simply 0.003. In general I can figure th=
is out with some IFs and stuff like that, but I'm hoping there is a more "e=
legant" way for this.
>=20
>=20
>=20
> 2) Long version: I'm analyzing some images and I get results with errors.=
These numbers are floating point and I would like to format the output so =
that it looks something like this: 'My results are (0.123 +/- 0.003) units'=
and the source data is:
>=20
> Result =3D 0.123456789
>=20
> Error =3D 0.003456789
>=20
> The point is that I need to find the first non-zero element in Error. The=
n I would use this number in the FORMAT parameter like this: MyFormat =3D '=
(f0'+STRTRIM(FirstNonZeroElement+2,2)+'.'+STRTRIM(FirstNonZeroElement,2)+')=
'
>=20
> and use "MyFormat" as formatting for the string conversion of both.
>=20
> (in the above example FirstNonZeroElement should be 3 and MyFormat would =
be '(f05.3)'.

ALOG10(ABS(X)) should give you this information.  But to be general, you ne=
ed to handle ABS(X) LT 1 and ABS(X) GT 1 separately.  And also, the X LT 0 =
case demands one extra digit for the '-' symbol. =20

Some extra rounding logic may be needed.

Craig
```
 0
Reply craig.markwardt (186) 8/7/2012 3:37:31 PM

```On Tuesday, August 7, 2012 5:37:31 PM UTC+2, Craig Markwardt wrote:
> On Tuesday, August 7, 2012 9:33:13 AM UTC-4, Helder wrote:
>=20
> > Dear all,
>=20
> >=20
>=20
> > well, I'm tangled up with something quite stupid, but I don't want to r=
einvent the wheel (for the 10th time today...).
>=20
> >=20
>=20
> > Here are two versions of the same question:
>=20
> >=20
>=20
> > 1) Short version: given a number such as 0.003456789 how do I get the m=
ost significant digit position? I need this number to produce formatted out=
put that would convert the number to simply 0.003. In general I can figure =
this out with some IFs and stuff like that, but I'm hoping there is a more =
"elegant" way for this.
>=20
> >=20
>=20
> >=20
>=20
> >=20
>=20
> > 2) Long version: I'm analyzing some images and I get results with error=
s. These numbers are floating point and I would like to format the output s=
o that it looks something like this: 'My results are (0.123 +/- 0.003) unit=
s' and the source data is:
>=20
> >=20
>=20
> > Result =3D 0.123456789
>=20
> >=20
>=20
> > Error =3D 0.003456789
>=20
> >=20
>=20
> > The point is that I need to find the first non-zero element in Error. T=
hen I would use this number in the FORMAT parameter like this: MyFormat =3D=
'(f0'+STRTRIM(FirstNonZeroElement+2,2)+'.'+STRTRIM(FirstNonZeroElement,2)+=
')'
>=20
> >=20
>=20
> > and use "MyFormat" as formatting for the string conversion of both.
>=20
> >=20
>=20
> > (in the above example FirstNonZeroElement should be 3 and MyFormat woul=
d be '(f05.3)'.
>=20
>=20
>=20
> ALOG10(ABS(X)) should give you this information.  But to be general, you =
need to handle ABS(X) LT 1 and ABS(X) GT 1 separately.  And also, the X LT =
0 case demands one extra digit for the '-' symbol. =20
>=20
>=20
>=20
> Some extra rounding logic may be needed.
>=20
>=20
>=20
> Craig

Hi Craig,
thanks, that is what I was looking for.
Cheers,
Helder
```
 0
Reply helder5178 (48) 8/8/2012 8:20:28 PM

4 Replies
34 Views

9/17/2012 10:56:51 PM