Author Topic: What's your philosophy about constants?  (Read 13641 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: What's your philosophy about constants?
« Reply #30 on: March 26, 2022, 12:13:37 pm »
IF BALLPARK(program_calculation, 0.11111111111111117896 * 2, +-0.11111111 ) ???

Just to say that has me thinking about a programming language full of funny expressions and colloquialisms .

So many interests, so little time ...

When dealing with floating point values, what you're suggesting is actually the best way to check for values.

Instead of something like: IF x = 10 THEN....

It's often better to use something more like: IF ABS(x - 10) < 0.0000001 THEN....

Floating point math has rounding glitches by its very nature.  For instance, it's impossible to represent 1/10 in base-2 math, just as it's impossible to represent 1/3 in base 10 math.  1/3 in decimal format is 0.3333333333333333333333333333333333333333333....  and more 3s on to infinity!  We can't perfectly represent 1/3 as a decimal, and it's just as impossible to represent 1/10 in binary format.

Over time, these rounding errors can add up, and even though we expect 0.1 added together 100 times to become 10, it may instead be 9.99999999999998721 or such.  A check for IF x = 10 THEN... will fail to pass muster, so instead we check the value against a chosen level of tolerance.  If the value is > 9.999999999999999 and the value is less than 10.000000000000001 then...  we're going to count it as being 10 with floating point errors counted in!  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #31 on: March 26, 2022, 01:03:37 pm »
Makes me wonder how the human species managed to land anything on Mars ...

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: What's your philosophy about constants?
« Reply #32 on: March 26, 2022, 01:26:57 pm »
Makes me wonder how the human species managed to land anything on Mars ...

Simple, they used my string-math method! Oh wait, that's how Columbus discovered India, only it turned out to be America. Oh well, he identified the indigenous people as Indians, so that's all that really mattered. But the more important question is, why do people think if Martians existed, they would be smarter than humans? After all, they have antenna, and we have cable!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline CharlieJV

  • Newbie
  • Posts: 89
    • View Profile
Re: What's your philosophy about constants?
« Reply #33 on: March 26, 2022, 01:34:22 pm »
(SNIP!) why do people think if Martians existed, they would be smarter than humans? (SNIP!)

Pete

Now I have the movie "Paul" on the brain ...

(https://i.pinimg.com/236x/b4/d5/23/b4d5233a0ca7a162a066c4697b9275c3--lmao-aliens.jpg)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: What's your philosophy about constants?
« Reply #34 on: March 26, 2022, 04:04:29 pm »
@johnno56
One may argue that they are man made constructs invented to prolong the illusion of man's existence.

This "One" would ask... In which Universe?
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: What's your philosophy about constants?
« Reply #35 on: March 26, 2022, 05:32:46 pm »
Truly one, there would be no otherness to question.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: What's your philosophy about constants?
« Reply #36 on: March 26, 2022, 08:29:24 pm »
That one works, but I wonder if relying on the compiler to produce the value of some other calculated number to use as a constant could cause an inconsistency if one program was compiled on a 32-bit and another on a 64-bit system?

Pete

You are right, 32-bit machines are incapable of computing 64-bit values, just as Android phones don't do negative numbers, and your TV remote only deals with even numbers.  Be careful out there, guys.