## .NET Core - Raspbian or Win10IoT?

jahboater
Posts: 3625
Joined: Wed Feb 04, 2015 6:38 pm

### Re: .NET Core - Raspbian or Win10IoT?

Heater wrote:
Sun Jun 24, 2018 12:10 pm
Then there is the whole floating point fiasco. To quote the common example in JS:

Code: Select all

``````> 0.3 + 0.3 + 0.3 == 0.9
false
``````
Which is not a JS problem as such, it's common to all languages using IEEE floats.
Well the trouble is here we are using binary digital computers. An analogue computer might fare better, or a digital computer that works in decimal.
0.3 cannot be represented in binary, we get 0x1.3333333333333p-2
You can see it is recurring and would require infinite precision to represent all of it. That cannot happen, so it doesn't add up.

We now have Decimal floating point in C and YES!! it works 0.3+0.3+0.3==0.9!!!!!!!!!!!!!!

Code: Select all

``````int
main( int argc, const char *argv[] )
{
_Decimal64 a, b, c, result;
a = 0.3DD;
b = 0.3DD;
c = 0.3DD;
result = 0.9DD;
printf( "a+b+c==result is %d\n", a+b+c == result );
}
``````
This prints 1. If I change it to double, then it prints 0.

This might be good for "currency" arithmetic which is always a problem in binary.
It is part of IEEE 754-2008 and IEC 60559.

In C++ this is "std::decimal::decimal64" or "__decfloat64" I am not sure which.

Heater
Posts: 10857
Joined: Tue Jul 17, 2012 3:02 pm

### Re: .NET Core - Raspbian or Win10IoT?

jahboater,

Yes. Problem is we are trying to describe the infinite detail of the real number line in the finite space of the computer.

But wait, that cannot be. Every schoolboy knows that:

0.3 + 0.3 + 0.3 =

3/10 + 3/10 + 3/10 =

(3 + 3 + 3) / 10 =

9 / 10 =

0.9

Every schoolboy has a finite sized brain so this computation is not impossible to do accurately.

No, fact is, computers cannot do arithmetic.

That _Decimal64 thing is great and all. Except that it does not work either:

Code: Select all

``````#include<stdio.h>

int main( int argc, const char *argv[] )
{
_Decimal64 a, b, c, result;
a = 1/3;
b = 1/3;
c = 1/3;
result = 1;
printf( "a+b+c==result is %d\n", a+b+c == result );
}
``````
Produces: "a+b+c==result is 0"

Again, every schoolboy with his finite sized brain knows that:

1/3 + 1/3 + 1/3 =

(1 + 1 + 1)/3 =

3 / 3 =

1

Computers cannot do arithmetic.

jahboater
Posts: 3625
Joined: Wed Feb 04, 2015 6:38 pm

### Re: .NET Core - Raspbian or Win10IoT?

Heater wrote:
Sun Jun 24, 2018 2:45 pm
int main( int argc, const char *argv[] )
{
_Decimal64 a, b, c, result;
a = 1/3;
b = 1/3;
c = 1/3;
result = 1;
printf( "a+b+c==result is %d\n", a+b+c == result );
}
[/code]
You have done it in binary (1/3 will be binary which then gets converted to decimal to be saved in the variables).

But it won't work in decimal either because every schoolboy knows that 1.0/3.0 is recurring.
It obviously cannot be represented (with finite precision) and therefore cannot add up to 1.0.
Unless you keep everything as fractions (and computers can do that too).
This is not a failing of computers, it is just plain maths.

BTW for decimal literals the suffix is DD for 64-bits, DF for 32-bits, and DL for 128-bits.

So it is 1.0DD/3.0DD
But as I said, since that is recurring, it wont work.

Do not compare floats without fully understanding whats going on

pauliunas
Posts: 41
Joined: Mon Feb 26, 2018 7:43 am

### Re: .NET Core - Raspbian or Win10IoT?

Heater wrote:
Sun Jun 24, 2018 2:45 pm
jahboater,

Yes. Problem is we are trying to describe the infinite detail of the real number line in the finite space of the computer.

But wait, that cannot be. Every schoolboy knows that:

0.3 + 0.3 + 0.3 =

3/10 + 3/10 + 3/10 =

(3 + 3 + 3) / 10 =

9 / 10 =

0.9

Every schoolboy has a finite sized brain so this computation is not impossible to do accurately.

No, fact is, computers cannot do arithmetic.

That _Decimal64 thing is great and all. Except that it does not work either:

Code: Select all

``````#include<stdio.h>

int main( int argc, const char *argv[] )
{
_Decimal64 a, b, c, result;
a = 1/3;
b = 1/3;
c = 1/3;
result = 1;
printf( "a+b+c==result is %d\n", a+b+c == result );
}
``````
Produces: "a+b+c==result is 0"

Again, every schoolboy with his finite sized brain knows that:

1/3 + 1/3 + 1/3 =

(1 + 1 + 1)/3 =

3 / 3 =

1

Computers cannot do arithmetic.
neither can computers browse the internet or anything like that. That's what programmers are for.

Heater
Posts: 10857
Joined: Tue Jul 17, 2012 3:02 pm

### Re: .NET Core - Raspbian or Win10IoT?

jahboater,

Yes I'm aware of the limitations of our computers binary representation. For ints and floats.

I should not say "fully aware" mind. I seem to find more anomalies every year. For example IEEE floats do not obey the arithmetic law of commutation:

a + b + c may not be the same as a + c + b

Nor the associative law:

a + (b + c) may not be the same as (a + b) + c

A project manager of mine at Marconi Radar R&D, back in 1980 something, told a new boy on the project: "If you think you need floating point to solve the problem, then you don't understand the problem"

The problem is perhaps in the languages, not the computers. Every schoolboy knows when fractions are recurring in decimal. But they also know how to get exact results when dealing with 1/3 etc. Which our computers do not.

When you read the text of such programs, as a schoolboy, you might expect the machine to do what you do.
This is not a failing of computers, it is just plain maths
It is a failing of computers, they cannot do plain maths. See my examples above.

Or perhaps it's a failing of us to expect that machines can do "plain maths" when they clearly cannot.
Unless you keep everything as fractions (and computers can do that too).
Keeping everything as rationals is great, provided you have the space for it, except they then explode when trying to calculate root 2 and such:

sqrt(2) * sqrt(2) might not be exactly 2.

You just can't win.

jahboater
Posts: 3625
Joined: Wed Feb 04, 2015 6:38 pm

### Re: .NET Core - Raspbian or Win10IoT?

Heater wrote:
Sun Jun 24, 2018 4:02 pm
It is a failing of computers, they cannot do plain maths.
Mine can. 2 + 2 always equals 4 (and for that matter 2.0 + 2.0 always equals 4.0 - and so floats may be safely compared for equality).

Anything else is ill-formed IMHO

Posts: 2274
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

### Re: .NET Core - Raspbian or Win10IoT?

but there are symbolic math libraries that cope with this kind of (notational finesse) thing fine, but at cost of processing time. There will be one for javascript if you want to have a play but to see what they do try typing
(1/3 + 1/3 + 1/3) == 1
or
sqrt(2) * sqrt(2)
into wolfram alpha

PS I was very impressed with the speed of your js random number application

Heater
Posts: 10857
Joined: Tue Jul 17, 2012 3:02 pm

### Re: .NET Core - Raspbian or Win10IoT?

jahboater,
Anything else is ill-formed IMHO
You are such a tease

...into wolfram alpha
Nah, that does not work either.

Believe in the math, not wolframalpha: https://www.youtube.com/watch?v=5pa1AryylpM&t=2s

Just accept it. Computers cannot do maths, or even arithmetic reliably. If you want to use them to help you have to be carefull.
PS I was very impressed with the speed of your js random number application
Thanks but it was not me. It was those clever guys at Google building the V8 JS engine.

Posts: 2274
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

### Re: .NET Core - Raspbian or Win10IoT?

On "computers can't do..." listened to a nice monologue by Will Self while driving this morning https://www.bbc.co.uk/programmes/b0b6pjh5 Basically when we realize computers can do everything we can but better it will be a bit of a let down rather than an apocalypse.

PS also I kind of expect programs like wolfram alpha to fail to solve difficult equations. But it actually does get the right answer - he just didn't click on the option to show the real root! see here

Heater
Posts: 10857
Joined: Tue Jul 17, 2012 3:02 pm

### Re: .NET Core - Raspbian or Win10IoT?

Thanks for the link. That was great.

Kind of ironic that a guy named Will Self is expounding on "self" and "will" like that.

Having read the works of Daniel Dennett, Douglas Hofstadter and the like since I was a kid I don't find anything new in the current debates about AI.

As it stands computers cannot do maths or even arithmetic reliably. Not from simple expressions in C to the symbolic crunching of Wolfram. See above.

Perhaps we can hope that AI and deep learning, or whatever it will be called next, will one day be able to get the results of a highschool student when looking at a maths problem. Sometimes right, sometimes wrong

jahboater
Posts: 3625
Joined: Wed Feb 04, 2015 6:38 pm

### Re: .NET Core - Raspbian or Win10IoT?

Heater wrote:
Sun Jun 24, 2018 6:11 pm
As it stands computers cannot do maths or even arithmetic reliably. Not from simple expressions in C to the symbolic crunching of Wolfram.
you are only unhappy because C gets 0.3 + 0.3 + 0.3 == 0.9 right but Javascript cant

Heater
Posts: 10857
Joined: Tue Jul 17, 2012 3:02 pm

### Re: .NET Core - Raspbian or Win10IoT?

Tease.

Not at all. Both JS and C have the same problems with IEEE floats. As do many other languages.

As far as I can make out _Decimal64 and such are not even part of the C standard.