[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Comparing two floats to the same number of significant figures

JensB

7/22/2011 9:18:00 AM

If I have two floating point numbers, what would be the easiest and
best (in terms of speed - this will be in a loop of up to 5000 items)
way of comparing them based on the number with the least significant
figures?

In other words
Comp 1.234 and 1.2343 will be equal
Comp 123000 and 12344.5 will be equal
Comp 123456 and 123450 will not

Etc.

Note that I do not know the number of sig figures beforehand...

--
Michael Cole


13 Answers

Dee Earley

7/22/2011 10:43:00 AM

0

On 22/07/2011 10:18, Michael Cole wrote:
> If I have two floating point numbers, what would be the easiest and best
> (in terms of speed - this will be in a loop of up to 5000 items) way of
> comparing them based on the number with the least significant figures?

Subtract one from the other and see how big the result is. You can get
your comparison value by doing 10^X.

I don't know how well this performs though.

--
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Jim Mack

7/22/2011 10:51:00 AM

0

Michael Cole wrote:
> If I have two floating point numbers, what would be the easiest and
> best (in terms of speed - this will be in a loop of up to 5000
> items) way of comparing them based on the number with the least
> significant figures?
>
> In other words
> Comp 1.234 and 1.2343 will be equal
> Comp 123000 and 12344.5 will be equal
> Comp 123456 and 123450 will not
>
> Etc.
>
> Note that I do not know the number of sig figures beforehand...

That sounds loopy. (-:

I thought I understood what you wanted until I saw your examples. I
can't see how all of those examples fit any one rule. Could you
explain further?

In example two, 123000 has 6 sig figs, as does its comparand. But they
differ by an order of magnitude. In what sense are they equal?

Can you show (in pseudocode) the method that you're using now, to help
clear that up?

--
Jim

Jeff Johnson [MVP: VB]

7/22/2011 2:27:00 PM

0

"Michael Cole" <invalid@microsoft.com> wrote in message
news:j0bf70$nbh$1@dont-email.me...

> If I have two floating point numbers, what would be the easiest and best
> (in terms of speed - this will be in a loop of up to 5000 items) way of
> comparing them based on the number with the least significant figures?
>
> In other words
> Comp 1.234 and 1.2343 will be equal
> Comp 123000 and 12344.5 will be equal
> Comp 123456 and 123450 will not
>
> Etc.
>
> Note that I do not know the number of sig figures beforehand...

One hundred twenty-three thousand will be considered equal to TWELVE
thousand three hundred (and change) just because the first three non-zero
digits are the same? What kind of goofy comparison are you doing? And at
this point the only answer is my mind is "turn 'em into strings!"


JensB

7/25/2011 1:30:00 AM

0

Correction for previous messages...

It happens that Michael Cole formulated :
> If I have two floating point numbers, what would be the easiest and best (in
> terms of speed - this will be in a loop of up to 5000 items) way of comparing
> them based on the number with the least significant figures?
>
> In other words
> Comp 1.234 and 1.2343 will be equal

Corection here: --
> Comp 123000 and 123445.5 will be equal
---

> Comp 123456 and 123450 will not
>
> Etc.
>
> Note that I do not know the number of sig figures beforehand...

--
Michael Cole


JensB

7/25/2011 2:16:00 AM

0

Jim Mack laid this down on his screen :
> Michael Cole wrote:
>> If I have two floating point numbers, what would be the easiest and
>> best (in terms of speed - this will be in a loop of up to 5000
>> items) way of comparing them based on the number with the least
>> significant figures?
>>
>> In other words
>> Comp 1.234 and 1.2343 will be equal
>> Comp 123000 and 12344.5 will be equal
>> Comp 123456 and 123450 will not
>>
>> Etc.
>>
>> Note that I do not know the number of sig figures beforehand...
>
> That sounds loopy. (-:
>
> I thought I understood what you wanted until I saw your examples. I
> can't see how all of those examples fit any one rule. Could you
> explain further?
>
> In example two, 123000 has 6 sig figs, as does its comparand. But they
> differ by an order of magnitude. In what sense are they equal?

Error in description - line should read

Comp 123000 and 123444.5 will be equal

123000 actually only has 3 sig figures, so both should round to 123000.

> Can you show (in pseudocode) the method that you're using now, to help
> clear that up?

I currently haven't implemented any.

--
Michael Cole


JensB

7/25/2011 2:18:00 AM

0

It happens that Jeff Johnson formulated :
> "Michael Cole" <invalid@microsoft.com> wrote in message
> news:j0bf70$nbh$1@dont-email.me...
>
>> If I have two floating point numbers, what would be the easiest and best
>> (in terms of speed - this will be in a loop of up to 5000 items) way of
>> comparing them based on the number with the least significant figures?
>>
>> In other words
>> Comp 1.234 and 1.2343 will be equal
>> Comp 123000 and 12344.5 will be equal
>> Comp 123456 and 123450 will not
>>
>> Etc.
>>
>> Note that I do not know the number of sig figures beforehand...
>
> One hundred twenty-three thousand will be considered equal to TWELVE thousand
> three hundred (and change)

No it won't. I made a mistake in typing the values in.
123000 will be comparable to 123444.5

--
Michael Cole


(nobody)

7/25/2011 3:19:00 AM

0

"Michael Cole" <invalid@microsoft.com> wrote in message
news:j0igsv$ehb$1@dont-email.me...
> Correction for previous messages...
>
> It happens that Michael Cole formulated :
>> If I have two floating point numbers, what would be the easiest and best
>> (in terms of speed - this will be in a loop of up to 5000 items) way of
>> comparing them based on the number with the least significant figures?
>>
>> In other words
>> Comp 1.234 and 1.2343 will be equal
>
> Corection here: --
>> Comp 123000 and 123445.5 will be equal
> ---
>
>> Comp 123456 and 123450 will not
>>
>> Etc.
>>
>> Note that I do not know the number of sig figures beforehand...
>
> --
> Michael Cole

The third case still looks puzzling.



JensB

7/25/2011 3:48:00 AM

0

Nobody laid this down on his screen :
> "Michael Cole" <invalid@microsoft.com> wrote in message
> news:j0igsv$ehb$1@dont-email.me...
>> Correction for previous messages...
>>
>> It happens that Michael Cole formulated :
>>> If I have two floating point numbers, what would be the easiest and best
>>> (in terms of speed - this will be in a loop of up to 5000 items) way of
>>> comparing them based on the number with the least significant figures?
>>>
>>> In other words
>>> Comp 1.234 and 1.2343 will be equal
>>
>> Corection here: --
>>> Comp 123000 and 123445.5 will be equal
>> ---
>>
>>> Comp 123456 and 123450 will not
>>>
>>> Etc.
>>>
>>> Note that I do not know the number of sig figures beforehand...
>>
>> -- Michael Cole
>
> The third case still looks puzzling.

123450 has 5 sig figures
123456 to 5 sig figures is 123460

The following can be used to calculate a number to the appropriate sig
figures: -

Round(Number * 10 ^ ((NumOfFigures - 1) - Int(Log10(Abs(Number))))) *
10 ^ (Int(Log10(Abs(Number))) - (NumOfFigures - 1)) * Sgn(Number)


I do not know how efficient this is, but it does give the correct
results.

Also note that part of the issue is determining the number of
significant figures. At the moment, about the only way I can think of
doing it is to perform a string comparison, starting from the right and
checking for non-zeroes.

--
Michael Cole


Dee Earley

7/25/2011 8:51:00 AM

0

On 25/07/2011 04:47, Michael Cole wrote:
> 123450 has 5 sig figures
> 123456 to 5 sig figures is 123460

No it's not.. Only if you arbitrarily round up, but that wasn't
mentioned in the original question.
(Comparing significant values shouldn't round IMO)

--
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk...

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

JensB

7/26/2011 1:16:00 AM

0

Deanna Earley wrote :
> On 25/07/2011 04:47, Michael Cole wrote:
>> 123450 has 5 sig figures
>> 123456 to 5 sig figures is 123460
>
> No it's not.. Only if you arbitrarily round up, but that wasn't mentioned in
> the original question.
> (Comparing significant values shouldn't round IMO)

From
<http://en.wikipedia.org/wiki/Significant_f...

....
Round up by one if appropriate. For example, if rounding 0.039 to 1
significant digit, the result would be 0.04.
....

I can find other (non-wikipedia) sources, but as far as I know, you
should always round.

Here's another

<http://www.staff.vu.edu.au/mcaonline/units/numbers/numsi...

--
Michael Cole