Skip to main content
Simplify spelling of arithmetic operations
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$31² (\$961\$961) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$45.6976 seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... % 10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$mod 2⁶⁴ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... % 10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That 31² (961) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant 45.6976 seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... % 10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing mod 2⁶⁴ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

added 1 character in body
Source Link
J_H
  • 42.3k
  • 3
  • 38
  • 157

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... %10_000% 10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... %10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... % 10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

added 727 characters in body
Source Link
J_H
  • 42.3k
  • 3
  • 38
  • 157

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... %10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

How would you rate it?

Obscure.

This does not impress me as a supportable codebase.

    hash=$((hash+(multiplier*(j-97)*961)))
    ...
  hash=$((((10_000_000*hash) / 456_976_000) % 10_000))

There's a lot of magic constants in the OP code. I imagine there might be some paper in the open literature that this code is based on. Cite your reference. What .PDF or other work does this source code rely upon?

That \$31^2\$ (\$961\$) left shift does not seem well motivated. Including unit tests which demonstrate how bash arithmetic behaves during deliberate overflow would be useful. One day bash might change such behavior, and you'll want to know how to diagnose it.

The constant \$45.6976\$ seems to have come from nowhere.

(And no, I didn't downvote. I did say the code seemed harder to maintain than necessary, and explained why.)


hash = ... %10_000))

The modulo makes it clear that we shall obey the 4-digit contract. It would be easier to reason about the loop if a modulo happened within it. From the Review Context my understanding is that we expect a "long" input string to deliberately overflow, producing \$\mod 2^{64}\$ results on each iteration. Coding an explicit modulo call would make Author's Intent more transparent, and reduce coupling between this high level source and low level details like bash version or native processor word width.

fix typo in a numeric constant
Source Link
J_H
  • 42.3k
  • 3
  • 38
  • 157
Loading
added 268 characters in body
Source Link
J_H
  • 42.3k
  • 3
  • 38
  • 157
Loading
Source Link
J_H
  • 42.3k
  • 3
  • 38
  • 157
Loading