Match part of the calculated value

Hi,
I have a symbol market value which changes dynamically & i have to
multiple it with static value order.

eg:
Symbol = 567.45
order = 10
Gross = symbol * order
Gross1 = 567.45 * 10 = 5674.50

In couple of seconds Symbol value might change to 567.88
So Gross2 = 567.88 * 10 = 5678.80

I wanted to compare if Gross1 and Gross2 are equal or if Gross1 has
values matching with Gross2.

Gross1 =~ /#{Gross2}/
This throws as nil for me, even if first 3 values are matching, i should
consider it was true match…how do i do that?

Hi,

ideal one wrote in post #1066196:

Gross1 =~ /#{Gross2}/
This throws as nil for me, even if first 3 values are matching, i should
consider it was true match…

Using the =~ operator on an Integer makes no sense, because the operator
isn’t really defined for Integers. It uses the dummy definition of
Object#=~, which simply returns nil and does nothing else.

But even if you convert the Integer to a String, it will get you
nowhere, because that’s just not how regular expressions work. You’ll
probably have to convert Gross1 to a String and go through each digit
(each_char), checking if Gross2 has the same digit (the decimal point
has to be skipped).

However, I’m not even sure how you want to compare the values:

gross1 = 414.23
gross2 = 542.14

How many matches do we have? 2? 3? Or more?

Please note: Don’t use capitalized names. Ruby interprets them as
constants (which is probably not what you want).

On Tue, Jun 26, 2012 at 9:45 PM, ideal one [email protected] wrote:

In couple of seconds Symbol value might change to 567.88
So Gross2 = 567.88 * 10 = 5678.80

I wanted to compare if Gross1 and Gross2 are equal or if Gross1 has
values matching with Gross2.

“Equal” does make sense for decimal numbers, but what do you mean by
“Gross1 has values matching with Gross2”? I cannot connect any math
operation with that.

Kind regards

robert

On Tue, Jun 26, 2012 at 10:47 PM, ideal one [email protected]
wrote:

Hi,
Actually if you see my gross1 and gross2 values, the differences are
only at decimal level.
the first 3 numbers are in most cases matching.
So maybe i should go for 3 number/character match depending whether i am
retrieving it as a number or a string.

I would not do any string comparison. The usual way is to define
either a max difference or a max factor between two values.

irb(main):001:0> gross1 = 567.45 * 10
=> 5674.5
irb(main):002:0> gross2 = 567.88 * 10
=> 5678.8
irb(main):003:0> (gross2 - gross1).abs
=> 4.300000000000182
irb(main):004:0> (gross2 - gross1).abs < 5
=> true
irb(main):005:0> ([gross1, gross2].max / [gross1, gross2].min)
=> 1.000757776015508
irb(main):006:0> ([gross1, gross2].max / [gross1, gross2].min) < 1.1
=> true

Since we’re talking about currency values: note also there is
BigDecimal for higher precision:

irb(main):007:0> require ‘bigdecimal’
=> true
irb(main):008:0> gross1 = BigDecimal(‘567.45’)*10
=> #BigDecimal:202be524,‘0.56745E4’,18(45)
irb(main):009:0> gross2 = BigDecimal(‘567.88’)*10
=> #BigDecimal:202bbdec,‘0.56788E4’,18(45)
irb(main):010:0> (gross2 - gross1).abs
=> #BigDecimal:202b7990,‘0.43E1’,18(36)
irb(main):011:0> (gross2 - gross1).abs.to_s
=> “0.43E1”
irb(main):012:0> (gross2 - gross1).abs < 5
=> true
irb(main):013:0> ([gross1, gross2].max / [gross1, gross2].min)
=> #<BigDecimal:202aa420,‘0.1000757776 0155079742 70860869E1’,36(45)>
irb(main):014:0> ([gross1, gross2].max / [gross1, gross2].min).to_s
=> “0.1000757776015507974270860869E1”
irb(main):015:0> ([gross1, gross2].max / [gross1, gross2].min) < 1.1
=> true

Cheers

robert

Super Robert…this is close to what i want.

thanks

=> “0.43E1”
irb(main):012:0> (gross2 - gross1).abs < 5
=> true
irb(main):013:0> ([gross1, gross2].max / [gross1, gross2].min)
=> #<BigDecimal:202aa420,‘0.1000757776 0155079742 70860869E1’,36(45)>
irb(main):014:0> ([gross1, gross2].max / [gross1, gross2].min).to_s
=> “0.1000757776015507974270860869E1”
irb(main):015:0> ([gross1, gross2].max / [gross1, gross2].min) < 1.1
=> true

Cheers

robert

Hi,
Actually if you see my gross1 and gross2 values, the differences are
only at decimal level.
the first 3 numbers are in most cases matching.
So maybe i should go for 3 number/character match depending whether i am
retrieving it as a number or a string.

gross1 = 567.45 * 10
gross2 = 567.88 * 10

So if i can say gross2 contains some numbers from gross1 in the same
order, that will solve my problem.
So based on above values,
gross1 = 5674.50
gross2 = 5678.80

567 is the common factor/contains in the both the gross1 & gross2.

Sorry if my explanation is confusing.

Thanks

Jan E. wrote in post #1066200:

Hi,

ideal one wrote in post #1066196:

Gross1 =~ /#{Gross2}/
This throws as nil for me, even if first 3 values are matching, i should
consider it was true match…

Using the =~ operator on an Integer makes no sense, because the operator
isn’t really defined for Integers. It uses the dummy definition of
Object#=~, which simply returns nil and does nothing else.

But even if you convert the Integer to a String, it will get you
nowhere, because that’s just not how regular expressions work. You’ll
probably have to convert Gross1 to a String and go through each digit
(each_char), checking if Gross2 has the same digit (the decimal point
has to be skipped).

However, I’m not even sure how you want to compare the values:

gross1 = 414.23
gross2 = 542.14

How many matches do we have? 2? 3? Or more?

Please note: Don’t use capitalized names. Ruby interprets them as
constants (which is probably not what you want).