Author Topic: Wasn't expecting this... C floating point arithmetic  (Read 14222 times)

0 Members and 1 Guest are viewing this topic.

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 785
  • Country: ca
Re: Wasn't expecting this... C floating point arithmetic
« Reply #75 on: April 03, 2018, 08:38:34 pm »
My OS periodically notifies me that "CPU microcodes were updated". Seems, we don't have hard-wired CPU /FPU /ALU anymore, but software-defined. This makes me think, that security issue concern may not be just floating point math related, but wider, you can not trust someone, who write those microcodes that integer math is not bugged. And reading assembler listing would not help much, since all low level instructions  like mov-mults-summ etc. may produce something that you would not expect.

How is that any different than hardware? The Pentium had a busted FPU in 1993, buggy CPUs aren't a new problem.
This is completely different matter. Hardware bugs you could discover by running test Before you set up multi-billion  account. And even if it was not discovered, than after crime happened, special task team could find a bug and use it as evidence.
Software microcode hack is much harder to prove. It could be remotely activated at specific time. You could have thousands  programmers verifying by hand &pencils every bit and line in the assembly code, but all this hard-work is useless. 
 

Offline paulcaTopic starter

  • Super Contributor
  • ***
  • Posts: 4046
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #76 on: April 03, 2018, 09:01:41 pm »
I think the point is the computer uses it's best skill, iterative, tedious computation.  A human with more mathematical wit than  me will realise they are quicker ways, that just require the kind of dynamic thought processes that computers don't have.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #77 on: April 03, 2018, 09:24:49 pm »
That's what JIT is for.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Wasn't expecting this... C floating point arithmetic
« Reply #78 on: April 03, 2018, 09:51:51 pm »
Quote
time for a new number format : Algebraic

It's not new.  Something like this dates back to the 70s... (Macsyma/Maxima.)

Code: [Select]
(%i12) 1/(2*%pi) + 1/(6*%pi);
                                       2
(%o12)                               -----
                                     3 %pi
 
The following users thanked this post: newbrain

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Wasn't expecting this... C floating point arithmetic
« Reply #79 on: April 03, 2018, 11:08:54 pm »
It is not really anything to do with floating point... you can't give precise answers to quite a few real-world questions, including calculating taxes.

For example here is an easy question:

If want to have $1000 in a savings account for Christmas, every christmas, what is the minimum I need to save each week?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Wasn't expecting this... C floating point arithmetic
« Reply #80 on: April 03, 2018, 11:11:57 pm »
It is not really anything to do with computers and floating point... you can't give precise answers to quite a few real-world questions, including calculating taxes.

For example here is an easy question:

If want to have $1000 in a savings account for Christmas, every Christmas, what is the minimum I need to save each week?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Wasn't expecting this... C floating point arithmetic
« Reply #81 on: April 03, 2018, 11:13:24 pm »
computer uses it's best skill, iterative, tedious computation.

What are you smoking? Seriously.

Quote
A human with more mathematical wit than me will realise they are quicker ways, that just require the kind of dynamic thought processes that computers don't have.

If you are trolling, then please stop because it's too much already.
Otherwise please update your knowledge about computers - what they are and how they work.
Hitchhiker's Guide to the Galaxy is wrong way to learn about computers.
Use proper books that are not science fiction, instead.
« Last Edit: April 03, 2018, 11:19:07 pm by ogden »
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Wasn't expecting this... C floating point arithmetic
« Reply #82 on: April 03, 2018, 11:32:01 pm »
It is not really anything to do with computers and floating point... you can't give precise answers to quite a few real-world questions, including calculating taxes, no matter how hard you try.

For example, here is an easy question:

If I want to have $1000 in a money jar for Christmas, every Christmas, how much do I need to put in the jar each week?

Any takers to not use a pesky, imprecise, error-prone computer to give a precise answer?

Something better than the obvious (and very practical) $20 per week?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: Wasn't expecting this... C floating point arithmetic
« Reply #83 on: April 04, 2018, 12:31:14 am »
If I want to have $1000 in a money jar for Christmas, every Christmas, how much do I need to put in the jar each week?

If your account doesn't earn any interest then the best way is to save the whole $1000 a week before Christmas. This way you enjoy free access to all of your money during the year.

If you do earn the interest, then the precise answer depends on the rules and rounding that your bank applies, and also on the overall amount of your income and your tax bracket, because your tax on the interest depends on it. Thus, the precise answer involves quite a bit of calculations.

 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Wasn't expecting this... C floating point arithmetic
« Reply #84 on: April 04, 2018, 02:28:33 am »
If your account doesn't earn any interest then the best way is to save the whole $1000 a week before Christmas. This way you enjoy free access to all of your money during the year.

If you do earn the interest, then the precise answer depends on the rules and rounding that your bank applies, and also on the overall amount of your income and your tax bracket, because your tax on the interest depends on it. Thus, the precise answer involves quite a bit of calculations.

If you did the calculations, could you give me a precise answer that cannot be proved wrong in at least some cases? (which is really the point of the question). I don't think you can.

Arithmetic and mathematics at two different things. Computers are good at arithmetic, but mathematics? Meh, not so much.

Computers also have a level of quantization error. It is the programmers job to manage that issue, and what works really well in one use-case is not guaranteed to work in others. Sometimes the errors matter, sometimes the don't.  Sometimes things will just not work, like defining PI as a constant, and then seeing that sin(PI*i) is never a constant value for all possible integer values of i (which is mathematically true).

The world is filled with uncertainty, and imprecise answers. Even the atomic weight of Carbon changes depending things...

Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline paulcaTopic starter

  • Super Contributor
  • ***
  • Posts: 4046
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #85 on: April 04, 2018, 09:17:33 am »
computer uses it's best skill, iterative, tedious computation.

What are you smoking? Seriously.

Quote
A human with more mathematical wit than me will realise they are quicker ways, that just require the kind of dynamic thought processes that computers don't have.

If you are trolling, then please stop because it's too much already.
Otherwise please update your knowledge about computers - what they are and how they work.
Hitchhiker's Guide to the Galaxy is wrong way to learn about computers.
Use proper books that are not science fiction, instead.

I'm sorry, who are you?

You are saying that a "computer"'s best feature is not it's ability to compute?  It's kind of what they are by definition, it's even its name.  They replaced people that were called computers to calculate artillery shell trajectories which was laborious, tedious and error prone.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19493
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Wasn't expecting this... C floating point arithmetic
« Reply #86 on: April 04, 2018, 10:05:02 am »
A submarine's best feature is its ability to swim underwater.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #87 on: April 04, 2018, 10:08:17 am »
@paulca: with you on this one.

Computers compute a tiny subset of all computations possible. Everything else is abstract and requires humans to map the abstraction onto the tiny subset. Sometimes someone manages to consolidate some of those abstractions into a program that generates the tiny subset (compiler / runtime / vm / CAS etc).

Computers are dumb as shit. Humans are the ones who are doing all the magic and leveraging the computer's advantage of doing simple and stupid things really quickly.
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Wasn't expecting this... C floating point arithmetic
« Reply #88 on: April 04, 2018, 10:32:50 am »
I'm sorry, who are you?

One who did not quite connect with your abstract language "computer uses it's best skill" :D

Quote
You are saying that a "computer"'s best feature is not it's ability to compute?

I did not say that.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14465
  • Country: fr
Re: Wasn't expecting this... C floating point arithmetic
« Reply #89 on: April 04, 2018, 11:24:30 am »
The main point lying not in what can or cannot do a computer, but in numerical vs. symbolic calculation.
It's common to confuse the two.

For instance, something that looks as benign as 1/3 is typically a symbolic representation of a rational number. The numerical representation we sometimes use to describe it, "0.3333...", is nothing more than a notation that exactly means that this is a rational number (and thus has an infinitely repeating decimal pattern), and is equivalent to writing 1/3. It bears no extra meaning. Now when we cut the decimals to a finite number of places, we get out of the symbolic realm and we transform it into an approximation.

Again, an FPU is designed for numerical approximate computation only. But a computer is perfectly capable of dealing with symbolic computation when programmed properly.
The fact that modern CPUs don't integrate built-in symbolic computation is just a matter of low demand and probably unjustified extra complexity.

Granted it could be nice to have at least built-in rational number support and this may not cost that much. But demand is most likely low enough that vendors don't care.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Wasn't expecting this... C floating point arithmetic
« Reply #90 on: April 04, 2018, 11:41:16 am »
Granted it could be nice to have at least built-in rational number support and this may not cost that much.
But all the interesting math is done using irrational numbers... :D
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Wasn't expecting this... C floating point arithmetic
« Reply #91 on: April 04, 2018, 11:46:00 am »
built-in symbolic computation is just a matter of low demand and probably unjustified extra complexity

There has always been a great demand for the world's definitive system for modern technical computing. Mathematica is a good approximation of such a will, and Worfram has always provided the best CAS ever available for a technology, e.g. they released it for Irix, when SGI was a good choice. Now they release for rpi@linux, to also keep high the interest for those, typically students, who can't afford the full purchase.

Opensource has never had any valid alternative.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14465
  • Country: fr
Re: Wasn't expecting this... C floating point arithmetic
« Reply #92 on: April 04, 2018, 12:05:29 pm »
Of course there is demand for symbolic computation tools. There just isn't any justified demand for that to get built-in, hard-wired into CPUs. And that wouldn't even make much sense.

 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14465
  • Country: fr
Re: Wasn't expecting this... C floating point arithmetic
« Reply #93 on: April 04, 2018, 12:09:10 pm »
But all the interesting math is done using irrational numbers... :D

That's a point of view. A lot more can be done with rational numbers than most people think. Define "interesting".  :-DD

Again, when approximations are not good enough for your particular needs when using irrational numbers (most often they are when used properly), you can use some kind of symbolic computation instead. Tools and libraries abound.
 

Offline free_electron

  • Super Contributor
  • ***
  • Posts: 8517
  • Country: us
    • SiliconValleyGarage
Re: Wasn't expecting this... C floating point arithmetic
« Reply #94 on: April 06, 2018, 04:18:43 pm »
Quote
time for a new number format : Algebraic

It's not new.  Something like this dates back to the 70s... (Macsyma/Maxima.)

Code: [Select]
(%i12) 1/(2*%pi) + 1/(6*%pi);
                                       2
(%o12)                               -----
                                     3 %pi
So how come there is not a standard library for this ? and we are all still effing around with ieee7-whatever ?
i'm willing to guess that banks are NOT using ieee format ... Actually visual basic has the 'currency' format. specifically to avoid these rounding error things. small things accumulate very quickly if you are doing millions of transactions a day ... banks don't like to lose money due to imprecise maths...
Professional Electron Wrangler.
Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19493
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: Wasn't expecting this... C floating point arithmetic
« Reply #95 on: April 06, 2018, 09:58:17 pm »
Quote
time for a new number format : Algebraic

It's not new.  Something like this dates back to the 70s... (Macsyma/Maxima.)

Code: [Select]
(%i12) 1/(2*%pi) + 1/(6*%pi);
                                       2
(%o12)                               -----
                                     3 %pi
So how come there is not a standard library for this ? and we are all still effing around with ieee7-whatever ?
i'm willing to guess that banks are NOT using ieee format ... Actually visual basic has the 'currency' format. specifically to avoid these rounding error things. small things accumulate very quickly if you are doing millions of transactions a day ... banks don't like to lose money due to imprecise maths...

Not quite. Banking calculations specify a particular rounding mode, one of many rounding modes.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4780
  • Country: pm
  • It's important to try new things..
Re: Wasn't expecting this... C floating point arithmetic
« Reply #96 on: April 06, 2018, 10:13:27 pm »
Banking apps do not use binary floating point. They use decimal floating point. There are CPUs with that option too (ie Power6/7/8/9/..)
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #97 on: April 06, 2018, 10:25:53 pm »
Banking apps do not use binary floating point. They use decimal floating point. There are CPUs with that option too (ie Power6/7/8/9/..)

Correct. Although perhaps strangely, a lot of the platforms that sit on the Z series (that aren’t using Java) don’t actually use the CPU support but use decNumber because it is portable and consistent across all architectures. No one has a z series on their desk...  : http://speleotrove.com/decimal/


Also that’s what the WP81 calculator uses, an HP calculator emulation on hardware. Interestingly HP used decimal arithmetic on their calculators too.
« Last Edit: April 06, 2018, 10:27:29 pm by bd139 »
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3146
  • Country: ca
Re: Wasn't expecting this... C floating point arithmetic
« Reply #98 on: April 06, 2018, 10:38:49 pm »
For banking applications, the best way is using integers representing the number of cents.

32-bit integers are not long enough to hold accounting numbers any more, but 64-bit integers still provide enough room, unless we get run-away inflation that is.
 

Offline bd139

  • Super Contributor
  • ***
  • Posts: 23018
  • Country: gb
Re: Wasn't expecting this... C floating point arithmetic
« Reply #99 on: April 06, 2018, 10:49:26 pm »
Number cents isn't enough precision sometimes. Some unit values are far less than one cent so you need variable precision. If you look at the method I described here, it allows variable precision with persistence:

https://www.eevblog.com/forum/microcontrollers/wasn_t-expecting-this-c-floating-point-arithmetic/msg1469544/#msg1469544
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf