Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Linear Zoetrope
Nov 28, 2011

A hero must cook
In Rust, the general solution is that you can elect to return Result<_,Box<Error>> where Box<Error> means it can be any error. This has some tradeoffs, though. Namely, you can't distinguish the type of the error anymore because it loses its metadata and just becomes a vtable to the Error implementation.

Unfortunately, the other solutions (such as designing your own error type that's an enum of the possible errors that could be returned) lose you the use of the ? operator making it unwieldy again.

Adbot
ADBOT LOVES YOU

vOv
Feb 8, 2014

TheBlackVegetable posted:

I'm not really suggesting using Result for numeric operations, it's more an example of how it could be used. Actually I am, in the sense it would work fine as a well designed feature of a language.

But I don't really understand the problem. It's easy enough to imagine a common situation where one function returns Result<'T1, 'E1>, and its caller returns Result <'T2, 'E2>, so it's something you'll need a general solution for anyway. I don't think I'd ever return a Result <Result<>>

The obvious solution then is to have some function of type E1 -> E2 so you can embed one error space inside the other. But it's really not clear to me how to embed a DivisionByZeroError inside the error space of, say, your built-in library function that opens a file.

VikingofRock
Aug 24, 2008




Linear Zoetrope posted:

Unfortunately, the other solutions (such as designing your own error type that's an enum of the possible errors that could be returned) lose you the use of the ? operator making it unwieldy again.

I thought you could still use the ? operator if you implement From<E> for your error enum, where E is the wrapped error?

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

vOv posted:

The obvious solution then is to have some function of type E1 -> E2 so you can embed one error space inside the other. But it's really not clear to me how to embed a DivisionByZeroError inside the error space of, say, your built-in library function that opens a file.

I think that discordance highlights the real issue. We shouldn't be using the same system to handle true faults (cannot allocate any more memory) as perfectly normal conditions (trying to parse a string to int that contains letters).

vOv
Feb 8, 2014

KernelSlanders posted:

I think that discordance highlights the real issue. We shouldn't be using the same system to handle true faults (cannot allocate any more memory) as perfectly normal conditions (trying to parse a string to int that contains letters).

Right, that's what I'm arguing.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


hackbunny posted:

Don't handle CPU faults as exceptions unless you want to add a lot of overhead to exceptions.

I assume you wouldn't suggest handling them as part of a Result type, which was the alternative under discussion.

Actually, what would you suggest, and what alternatives have been used? I've never worked with signals in a language that did it any other way.

JawnV6
Jul 4, 2004

So hot ...

Absurd Alhazred posted:

0x10000000 would be a slightly better candidate for signed integers; no problem with 0xFFFFFFFF for unsigned integers, +1 from that is overflow anyway.

What unholy significance are you assigning to bit 28 here? Surely you meant 0x80000000?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Don't mind me, I'll just be over here rewriting C's usual arithmetic conversion rules to make sizeof(int) + (-1) not fail catastrophically.

Coffee Mugshot
Jun 26, 2010

by Lowtax
How did a discussion about handling division errors in an ergonomic way for developers turn into this?

JawnV6
Jul 4, 2004

So hot ...

Coffee Mugshot posted:

How did a discussion about handling division errors in an ergonomic way for developers turn into this?

Christ, this is the entire point behind zero-cost abstractions. You don't get the context served up on a platter, if you need to know how we got here expect to unwind it yourself. Prepare metadata accordingly.

VikingofRock
Aug 24, 2008




JawnV6 posted:

Christ, this is the entire point behind zero-cost abstractions. You don't get the context served up on a platter, if you need to know how we got here expect to unwind it yourself. Prepare metadata accordingly.

:golfclap:

Coffee Mugshot
Jun 26, 2010

by Lowtax
That's pretty good but I still think that's more of an API problem than a language problem

Absurd Alhazred
Mar 27, 2010

by Athanatos

JawnV6 posted:

What unholy significance are you assigning to bit 28 here? Surely you meant 0x80000000?

That's what I meant. :sweatdrop:

Coffee Mugshot posted:

That's pretty good but I still think that's more of an API problem than a language problem

Yeah, we should have encoded version and structure type into the first 64-bit word to ensure forward compatibility.

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

Doc Hawkins posted:

I assume you wouldn't suggest handling them as part of a Result type, which was the alternative under discussion.

Actually, what would you suggest, and what alternatives have been used? I've never worked with signals in a language that did it any other way.

This is, or used to be, one of my areas of greatest expertise. We need to make a very important distinction first: handling faults vs catching faults

Take a relatively common example of user-mode handling of CPU faults: on-demand memory allocation. Let's say you reserve a large buffer in memory as a range of unallocated memory pages: as soon as code attempts to write to the buffer, the CPU faults on the missing page, and calls the user mode handler; for simplicity, let's assume it's the SIGSEGV handler in a C program. In the signal handler, the program looks at the faulting address (si_addr field of the signal information structure), and if it's within the range of the buffer, it allocates the page that was missing (let's say, with mmap), and resumes execution. This is an example of handling a fault: the faulting code doesn't even realize a fault occurred

Catching, on the other hand, is what you need for unrecoverable errors - for example, a division by zero, which can't be "fixed" in any way by the SIGFPE handler - when you don't want to terminate the whole process. You can do this on Windows (... and OS/2, and - !!! - Tru64 UNIX), with the non-standard __try/__except construct, which lets you catch CPU faults like they were language exceptions. Ideally, in Rust you'd add an equivalent construct that can capture the fault in a Result. But this is easier said than done...

Think about it: what does the fault catcher know about the code that faulted, except that somewhere down the stack a division by zero occurred? Absolutely nothing. The faulting code might have been in the middle of a larger operation, and by interrupting it, effectively forcing a recursive return statement at that point, you might have left some data in an undefined state. Sure, you have the __try/__finally statement, to clean up even when the code is interrupted by a fault, but to be on the safe side, you have to do all clean up with __try/__finally, in your entire program, external libraries and operating system binaries included. And do you know what happens to all code inside a __try? it must be compiled with the assumption that every single instruction can do the equivalent of throwing an exception, even NOPs (what if a fault occurs while fetching the NOP instruction itself?). In languages that implicitly add clean-up code, like C++ (see the RAII idiom), any function with implicit clean-up must be compiled like it was wrapped in a single big __try/__finally, or a fault might cause clean-up code to be skipped or - possibly even worse - be performed on not fully initialized data (in fact, it's more like several nested __try/__finally statements, one for each variable that needs to be destroyed). This interferes with compilers and especially optimizers so much that, even when it's supported, it's disabled by default: on gcc, you need to compile with -fasynchronous-exceptions; on Visual C++, with the equivalent /EHa; clang outright doesn't support it as it's completely unsupported by the backend

A function that can catch CPU faults in a Result would have to be explicitly marked as such, and it would have to ignore any faults raised by functions that weren't marked as fault-safe (or worse, raised by foreign code altogether), and let them crash the program. I'm unfamiliar with Rust but I don't see its strictness playing well with structured fault handling at all

hackbunny fucked around with this message at 13:56 on Sep 26, 2017

Vanadium
Jan 8, 2005

Rust just emits asserts against zero before all divisions.

Nova69
Jul 12, 2012

I prefer all my all my runtime errors to result in a full kernel panic, no possibility of lazy catching there :smug:

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
I use a custom kernel & hardware controller that fills my computer with jam whenever it encounters a runtime error

Coffee Mugshot
Jun 26, 2010

by Lowtax
Casual reminder of why git is a flawless VCS http://caiustheory.com/git-git-git-git-git/

Absurd Alhazred
Mar 27, 2010

by Athanatos

Coffee Mugshot posted:

Casual reminder of why git is a flawless VCS http://caiustheory.com/git-git-git-git-git/

Too many gits, too many gits...
Too many gits, toooo maannny giiiittss...

PhantomOfTheCopier
Aug 13, 2008

Pikabooze!

vOv posted:

'this program needs to die now' in case you do something like run out of memory
Clearly the working set sheriff should present the system administrator with a prompt to add more memory to the system :colbert: and refuse to continue operations until the modules have been inserted.

QuarkJets posted:

The fact that Matlab actually does the opposite of that (upcasting the operation and then downcasting the result to the smallest of the two precisions) is unreasonable and frankly just bizarre
The fact that a math focused application ensures results introduce no false precision is entirely reasonable and good. If x=pi is a single, y=pi is a double, then x+y can only ever be 2pi to the precision of a single. Returning x+y as a double is just wrong.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

PhantomOfTheCopier posted:

The fact that a math focused application ensures results introduce no false precision is entirely reasonable and good. If x=pi is a single, y=pi is a double, then x+y can only ever be 2pi to the precision of a single. Returning x+y as a double is just wrong.

Suppose we have a (double-precision) value of 2^53 units, and we add (single-precision) 2 to that. How precisely do we know the result?

Similarly, suppose we have two (double-precision) values of 2^53 and 2^53+2. How precisely do we know the difference between them?

PhantomOfTheCopier
Aug 13, 2008

Pikabooze!
Post the application in which that arises and you'll have supplied the thread with the coding horror.

lol Pluto's perturbations on Earth's tides are getting lost in a double.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Oh you're right, I can't imagine why anyone would ever care about small differences between large numbers. That must be some kind of epic coding horror and not a highly useful physical experiment at all!

fritz
Jul 26, 2003

Jabor posted:

Oh you're right, I can't imagine why anyone would ever care about small differences between large numbers. That must be some kind of epic coding horror and not a highly useful physical experiment at all!

On the other hand if you look at the actual data: http://polac.obspm.fr/llrdatae.html you'll note that the actual data looks like:
code:
  <TR>
    <TD>1969-08-20T02:56:11.9999990</TD>
    <TD>40453.1223611111</TD>
    <TD>2.4956468426</TD>
    <TD>0</TD>
    <TD>71110</TD>
    <TD>8</TD>
    <TD>99900.0</TD>
    <TD>0.0</TD>
    <TD>790.0</TD>
    <TD>285.1</TD>
    <TD>0</TD>
    <TD>694.3</TD>
    <TD>0</TD>
    <TD>ARCH-ZN</TD>
  </TR>
and the value measured is 2.4956468426 (ok the actual actual data is a flat file, but w/ever)

CPColin
Sep 9, 2003

Big ol' smile.
code:
catch (Exception ex)
{
    throw new Exception(ex.ToString());
}
:thunk:

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

CPColin posted:

code:
catch (Exception ex)
{
    throw new Exception(ex.getClass().toString());
}
:thunk:

okay, fixed it

CPColin
Sep 9, 2003

Big ol' smile.
Whew!

Zemyla
Aug 6, 2008

I'll take her off your hands. Pleasure doing business with you!

PhantomOfTheCopier posted:

The fact that a math focused application ensures results introduce no false precision is entirely reasonable and good. If x=pi is a single, y=pi is a double, then x+y can only ever be 2pi to the precision of a single. Returning x+y as a double is just wrong.

Clearly, we should represent all real values as lazy infinite continued-fraction lists.

QuarkJets
Sep 8, 2008

PhantomOfTheCopier posted:

The fact that a math focused application ensures results introduce no false precision is entirely reasonable and good. If x=pi is a single, y=pi is a double, then x+y can only ever be 2pi to the precision of a single. Returning x+y as a double is just wrong.

If an instrument records the number 1 as an 8-bit integer, by your logic any math that I do with that result should be restricted to 0-255. That's some broke-brain bullshit.

e: If you want your math- and science- focused language to be overly concerned with numerical precision based on the number of bits storing results, then you should spit out an error when two different precision levels clash; that lets the user decide whether they actually care about the numerical precision of all of the variables in that operation. Silently downcasting the result is the worst, least reasonable option. Downcasting while also spitting out a warning would at least tell the user that something has happened that they may not expect (since this behavior is counter-intuitive in all ways), but Matlab doesn't do that.

QuarkJets fucked around with this message at 21:25 on Sep 28, 2017

JawnV6
Jul 4, 2004

So hot ...

PhantomOfTheCopier posted:

Clearly the working set sheriff should present the system administrator with a prompt to add more memory to the system :colbert: and refuse to continue operations until the modules have been inserted.
Cool rehash of VMware ballooning.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Is there a name for variables whose types keep getting changed over the course of a program written in a dynamic language? If not, I want to call them "skinwalker variables." First it's a class type, then it's a dictionary, and later it breaks into your cabin and eats your face.

sarehu
Apr 20, 2007

(call/cc call/cc)
[quote="“PhantomOfTheCopier”" post="“476848009”"]
The fact that a math focused application ensures results introduce no false precision is entirely reasonable and good. If x=pi is a single, y=pi is a double, then x+y can only ever be 2pi to the precision of a single. Returning x+y as a double is just wrong.
[/quote]

Sorry but this is crazy and you are a crazy person. It's useful for intermediate calculations to use excess precision.

PhantomOfTheCopier
Aug 13, 2008

Pikabooze!
You people sure have some strange reading problems. Matlab is an application. It is perfectly reasonable for it to return calculations that adhere most closely to standard practice of significant digits. Nothing prevents and nothing I wrote prevents intermediate and internal calculation to be handled differently. Real number results are handled differently than integers (which I said nothing about). Nothing prevents an idiot with a computer programming language from specifically requesting the wrong value for 2pi. I'm sure some legislator would have been happy to have such an app.

As to the bot poster who replies immediately to every word I write, thank you for being a follower but a straw man argument simply will not do. Differences between large and small numbers are very important, and this I acknowledged in my original comment (which you apparently failed to actually read). Anyone who writes code to handle those situations, including moon mirror fluctuations, assuming that a double in a standard statically typed computer programming language (which is NOT a mathematics application, so different than what was being discussed) is sufficient for their needs will have their crappy code posted here.

Now I know why people like agile development; it's because they couldn't possibly write code based on a specification they'd have to read.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

PhantomOfTheCopier posted:

You people sure have some strange reading problems. Matlab is an application. It is perfectly reasonable for it to return calculations that adhere most closely to standard practice of significant digits. Nothing prevents and nothing I wrote prevents intermediate and internal calculation to be handled differently. Real number results are handled differently than integers (which I said nothing about).

2^30, as a double, has an absolute precision of ±2^-23. 4, as a single, has an absolute precision of ±2^-21. And yet if you add them together, you think that an absolute precision of ±2^7 is reasonable?

That's ultimately the point, that floating-point size has no correlation with the absolute precision of a measurement or result. Trying to justify rear end-backwards behaviour by claiming that it enforces correct precision is completely laughable.

PhantomOfTheCopier posted:

As to the bot poster who replies immediately to every word I write,

Get over yourself.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

PhantomOfTheCopier posted:

thank you for being a follower but a straw man argument simply will not do.

What about an ad hominem, then? Your posting is bad, lol.

Eela6
May 25, 2007
Shredded Hen

PhantomOfTheCopier posted:

You people sure have some strange reading problems. Matlab is an application. It is perfectly reasonable for it to return calculations that adhere most closely to standard practice of significant digits. Nothing prevents and nothing I wrote prevents intermediate and internal calculation to be handled differently. Real number results are handled differently than integers (which I said nothing about). Nothing prevents an idiot with a computer programming language from specifically requesting the wrong value for 2pi. I'm sure some legislator would have been happy to have such an app.

As to the bot poster who replies immediately to every word I write, thank you for being a follower but a straw man argument simply will not do. Differences between large and small numbers are very important, and this I acknowledged in my original comment (which you apparently failed to actually read). Anyone who writes code to handle those situations, including moon mirror fluctuations, assuming that a double in a standard statically typed computer programming language (which is NOT a mathematics application, so different than what was being discussed) is sufficient for their needs will have their crappy code posted here.

Now I know why people like agile development; it's because they couldn't possibly write code based on a specification they'd have to read.

you're real dumb, hth

The Fool
Oct 16, 2003


Coding horror more like coder horror

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
Let us never lose sight of the fact that Matlab is a terrible, awful programming language that only survives because it has so goddamn many scientific/mathematical libraries bolted onto it.

I suppose the takeaway lesson is that having a strong standard library does a lot to make a language attractive, and conversely, that languages with sparse standard libraries are not very appealing.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

TooMuchAbstraction posted:

I suppose the takeaway lesson is that having a strong standard library does a lot to make a language attractive, and conversely, that languages with sparse standard libraries are not very appealing.

unless it's javascript

Adbot
ADBOT LOVES YOU

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Plorkyeran posted:

unless it's javascript
in which case it's appalling.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply