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
Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal

leper khan posted:

all this cpp talk is making me miss it.


am i broken?
probably!

but we're all broken here, i think

Adbot
ADBOT LOVES YOU

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

holy poo poo hackbunny what the how do you do this

i can't even parse anything, my eyes just slide off the page

but at the same time, i think it's interesting. I think. Can't tell, dying

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Captain Foo posted:

holy poo poo hackbunny what the how do you do this

i can't even parse anything, my eyes just slide off the page

but at the same time, i think it's interesting. I think. Can't tell, dying

you'll be fine have another coffee :coffeepal:

Xarn
Jun 26, 2015

leper khan posted:

all this cpp talk is making me miss it.


am i broken?

Probably, but you are not alone in this. Come back to the dark side.

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

leper khan posted:

you'll be fine have another coffee :coffeepal:

:sludgepal:

coffeemaker metaprogramming

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
you don't need to reduce a 10k source file, that is quite reduced enough

the line i actually care about as a compiler writer is "don't make me configure and run your goddamned build". preprocessed source and a command line, please

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Redmark posted:

Coincidentally I've been trying to learn Rust by making a bignum thing specifically to implement Karatsuba.

It was so incredibly easier than when I did it in C++ for school that I don't think I'll ever use C++ again unless I have to. Also you get to parallelize the divide-and-conquer part with things like rayon basically for free which is cool.

here's what I ended up with. I am doing the powers wrong and I know there's gotta be some way to split an integer without converting it to a string (i tried calculating them magnitude just by dividing by 10 until I got to zero but that seemed even dumber than converting to a string).

code:
extern crate num;
use self::num::*;
use std::cmp;


pub fn mult(x: &BigInt, y: &BigInt) -> BigInt {
    // base case
    if (x < &BigInt::from(10)) || (y < &BigInt::from(10)) {
        return x * y;
    }

    let m = cmp::max(x.to_string().len(), y.to_string().len());
    let split = m / 2 as usize;

    let (high1, low1) = split_at(&x, split);
    let (high2, low2) = split_at(&y, split);

    // compute partial products
    let z0 = mult(&low1, &low2);
    let z1 = mult(&(&low1+&high1), &(&low2+ &high2));
    let z2 = mult(&high1, &high2);

    return (&z2 * num::pow(BigInt::from(10), 2 * split)) +
           ((&z1 - &z2 - &z0) * num::pow(BigInt::from(10), split)) +
           &z0;
}

#[test]
fn test_mult() {
    assert_eq!(mult(&BigInt::from(5), &BigInt::from(10)), BigInt::from(50));
    
    assert_eq!(mult(&BigInt::from(4205), &BigInt::from(421234)),
               BigInt::from(1771288970));
    
   let var1 = "3141592653589793238462643383279502884197169399375105820974944592"
        .parse::<BigInt>()
        .unwrap();
   
   let var2 = "2718281828459045235360287471352662497757247093699959574966967627"
        .parse::<BigInt>()
        .unwrap();
   let expected = "8539734222673567065463550869546574495034888535765114961879601127067743044893204848617875072216249073013374895871952806582723184";

   assert_eq!(mult(&var1, &var2).to_string(), expected);

}

fn split_at(num: &BigInt, n: usize) -> (BigInt, BigInt) {
    let s = num.to_string();
    let (a, b) = s.split_at(s.len() - n);
    let c: BigInt = a.parse::<BigInt>().unwrap();
    let d: BigInt = b.parse::<BigInt>().unwrap();
    return (c, d);

}
now i'm gonna use rayon to parallelize it. will post results. also i'm aware that i will get torn to shreds for what is surely very bad code.

DONT THREAD ON ME fucked around with this message at 00:03 on Dec 9, 2016

gonadic io
Feb 16, 2011

>>=
use the Mul trait

see here for an example for a Rational type: https://z0ltan.wordpress.com/2016/10/17/implementing-a-rational-type-in-rust/

Ator
Oct 1, 2005

i want to make a text roguelike game with a lisp

maybe this lisp:


because it is "the ultimate language"

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
I started reading that book when the humble bundle had it, and it is really really badly written. Like, clojure may be good or bad, but that book is lovely

CPColin
Sep 9, 2003

Big ol' smile.
I couldn't tell if it was just me being bad at Lisp-likes. It probably was, anyway.

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
I couldn't even tell if it taught clojure well, it was just the smuggest loving book in existence

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

MALE SHOEGAZE posted:

now i'm gonna use rayon to parallelize it. will post results. also i'm aware that i will get torn to shreds for what is surely very bad code.

code:
pub fn par_mult(x: &BigInt, y: &BigInt) -> BigInt {
// base case
    if (x < &BigInt::from(10)) || (y < &BigInt::from(10)) {
        return x * y;
    }

    let m = cmp::max(x.to_string().len(), y.to_string().len());
    let split = m / 2 as usize;

    let (high1, low1) = split_at(&x, split);
    let (high2, low2) = split_at(&y, split);

    // compute partial products
    let mut z0 = BigInt::from(0);
    let mut z1 = BigInt::from(0);
    let mut z2 = BigInt::from(0);
    
    rayon::join(|| z0 = par_mult(&low1, &low2),
                        || rayon::join(
                               || z1 = par_mult(&(&low1+&high1), &(&low2+ &high2)),
                               || z2 = par_mult(&high1, &high2)));
 

    return (&z2 * num::pow(BigInt::from(10), 2 * split)) +
        ((&z1 - &z2 - &z0) * num::pow(BigInt::from(10), split)) +
        &z0;
}

code:
test tests::test_mult ... ignored
test tests::bench_builtin       ... bench:         337 ns/iter (+/- 85)
test tests::bench_karatsuba     ... bench:     836,913 ns/iter (+/- 218,827)
test tests::bench_par_karatsuba ... bench:     581,647 ns/iter (+/- 15,533)
lol at the builtin being so much faster, but pretty cool that rayon makes it that easy to parallelize.

gonadic io posted:

use the Mul trait

see here for an example for a Rational type: https://z0ltan.wordpress.com/2016/10/17/implementing-a-rational-type-in-rust/

cool i'll take a look!

DONT THREAD ON ME fucked around with this message at 01:20 on Dec 9, 2016

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Asymmetrikon posted:

I couldn't even tell if it taught clojure well, it was just the smuggest loving book in existence

I'm actually really curious to check this book now

jony neuemonic
Nov 13, 2009

Ator posted:

i want to make a text roguelike game with a lisp

maybe this lisp:


because it is "the ultimate language"

common lisp is a bit of a shaggy dog language but there is an excellent, free book that covers it well. i find it more enjoyable to write than clojure, but clojure is probably easier to get up and running with if for no other reason than its community isn't dead.

fritz
Jul 26, 2003

Asymmetrikon posted:

it was just the smuggest loving book in existence

and they say clojure isn't a real lisp ...

Weekend Bridges
Apr 8, 2015

by Smythe

fritz posted:

and they say clojure isn't a real lisp ...

lol

Redmark
Dec 11, 2012

This one's for you, Morph.
-Evo 2013

MALE SHOEGAZE posted:

code:
pub fn par_mult(x: &BigInt, y: &BigInt) -> BigInt {
// base case
    if (x < &BigInt::from(10)) || (y < &BigInt::from(10)) {
        return x * y;
    }

    let m = cmp::max(x.to_string().len(), y.to_string().len());
    let split = m / 2 as usize;

    let (high1, low1) = split_at(&x, split);
    let (high2, low2) = split_at(&y, split);

    // compute partial products
    let mut z0 = BigInt::from(0);
    let mut z1 = BigInt::from(0);
    let mut z2 = BigInt::from(0);
    
    rayon::join(|| z0 = par_mult(&low1, &low2),
                        || rayon::join(
                               || z1 = par_mult(&(&low1+&high1), &(&low2+ &high2)),
                               || z2 = par_mult(&high1, &high2)));
 

    return (&z2 * num::pow(BigInt::from(10), 2 * split)) +
        ((&z1 - &z2 - &z0) * num::pow(BigInt::from(10), split)) +
        &z0;
}

code:
test tests::test_mult ... ignored
test tests::bench_builtin       ... bench:         337 ns/iter (+/- 85)
test tests::bench_karatsuba     ... bench:     836,913 ns/iter (+/- 218,827)
test tests::bench_par_karatsuba ... bench:     581,647 ns/iter (+/- 15,533)
lol at the builtin being so much faster, but pretty cool that rayon makes it that easy to parallelize.


cool i'll take a look!

Nice, I see about the same speedup factor from parallelizing it. Here's my version for comparison. It's my first crate so the code is probably even worse!

I really appreciated how the program either worked first try or told you how it failed. The first time I tried bignum operations I got absolutely murdered by off-by-one errors and integer overflows. In Rust the only runtime bugs I got were due to my not understanding how to do simple long multiplication :downs:

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Redmark posted:

Nice, I see about the same speedup factor from parallelizing it. Here's my version for comparison. It's my first crate so the code is probably even worse!

I really appreciated how the program either worked first try or told you how it failed. The first time I tried bignum operations I got absolutely murdered by off-by-one errors and integer overflows. In Rust the only runtime bugs I got were due to my not understanding how to do simple long multiplication :downs:

you've inspired me to maybe do the rest of the bignum class from scratch.

also, I laughed out loud when I saw that you chained the rayon call the same way I did.

VikingofRock
Aug 24, 2008




There really should be an obfuscated C++ contest. There's so much room for horrific poo poo with templates.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

VikingofRock posted:

There really should be an obfuscated C++ contest. There's so much room for horrific poo poo with templates.

Programming in C++ is an obfuscated C++ contest.

Beamed
Nov 26, 2010

Then you have a responsibility that no man has ever faced. You have your fear which could become reality, and you have Godzilla, which is reality.


VikingofRock posted:

There really should be an obfuscated C++ contest. There's so much room for horrific poo poo with templates.

This is what C++ deserves for accidentally being two languages. :smith:

VikingofRock
Aug 24, 2008




Beamed posted:

This is what C++ deserves for accidentally being two languages. :smith:

I mean hell, the first item in Effective C++ 3rd Edition is this:

Scott Meyers posted:

Item 1: View C++ as a federation of languages.

In the beginning, C++ was just C with some object-oriented features tacked on. Even C++’s original name, “C with Classes,” reflected this simple heritage.

As the language matured, it grew bolder and more adventurous, adopting ideas, features, and programming strategies different from those of C with Classes. Exceptions required different approaches to structuring functions (see Item 29). Templates gave rise to new ways of thinking about design (see Item 41), and the STL defined an approach to extensibility unlike any most people had ever seen. Today’s C++ is a multiparadigm programming language, one supporting a combination of procedural, object-oriented, functional, generic, and metaprogramming features. This power and flexibility make C++ a tool without equal, but can also cause some confusion. All the “proper usage” rules seem to have exceptions. How are we to make sense of such a language?

The easiest way is to view C++ not as a single language but as a federation of related languages. Within a particular sublanguage, the rules tend to be simple, straightforward, and easy to remember. When you move from one sublanguage to another, however, the rules may change. To make sense of C++, you have to recognize its primary sublanguages. Fortunately, there are only four:

  1. C. Way down deep, C++ is still based on C. Blocks, statements, the preprocessor, built-in data types, arrays, pointers, etc., all come from C. In many cases, C++ offers approaches to problems that are superior to their C counterparts (e.g., see Items 2 (alternatives to the preprocessor) and 13 (using objects to manage resources)), but when you find yourself working with the C part of C++, the rules for effective programming reflect C’s more limited scope: no templates, no exceptions, no overloading, etc.

  2. Object-Oriented C++. This part of C++ is what C with Classes was all about : classes (including constructors and destructors), encapsulation, inheritance, polymorphism, virtual functions (dynamic binding), etc. This is the part of C++ to which the classic rules for object-oriented design most directly apply.

  3. Template C++. This is the generic programming part of C++, the one that most programmers have the least experience with. Template considerations pervade C++, and it’s not uncommon for rules of good programming to include special template-only clauses (e.g., see Item 46 on facilitating type conversions in calls to template functions). In fact, templates are so powerful, they give rise
    to a completely new programming paradigm, template metaprogramming (TMP). Item 48 provides an overview of TMP, but unless you’re a hard-core template junkie, you need not worry about it. The rules for TMP rarely interact with mainstream C++ programming.

  4. The STL. The STL is a template library, of course, but it’s a very special template library. Its conventions regarding containers, iterators, algorithms, and function objects mesh beautifully, but templates and libraries can be built around other ideas, too. The STL has particular ways of doing things, and when you’re working with the STL, you need to be sure to follow its conventions.

Keep these four sublanguages in mind, and don’t be surprised when you encounter situations where effective programming requires that you change strategy when you switch from one sublanguage to another. For example, pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types, but when you move from the C part of C++ to Object-Oriented C++, the existence of user-defined constructors and destructors means that pass-by-reference-to-const is usually better. This is especially the case when working in Template C++, because there, you don’t even know the type of object you’re dealing with. When you cross into the STL, however, you know that iterators and function objects are modeled on pointers in C, so for iterators and function objects in the STL, the old C pass-by-value rule applies again. (For all the details on choosing among parameter-passing options, see Item 20.)

C++, then, isn’t a unified language with a single set of rules; it’s a federation of four sublanguages, each with its own conventions. Keep these sublanguages in mind, and you’ll find that C++ is a lot easier to understand.

Things to Remember
Rules for effective C++ programming vary, depending on the part of C++ you are using.

Incidentally I cannot recommend that book enough to someone actually interested in trying to understand the behemoth that is C++. The book's biggest problem is that it pre-dates C++11, but his more recent book Effective Modern C++ is a good supplement to get you up to speed on those features.

VikingofRock
Aug 24, 2008




The second biggest issue with the book is Scott Meyers's dorky haircut:

Nehacoo
Sep 9, 2011

VikingofRock posted:

I mean hell, the first item in Effective C++ 3rd Edition is this:


Incidentally I cannot recommend that book enough to someone actually interested in trying to understand the behemoth that is C++. The book's biggest problem is that it pre-dates C++11, but his more recent book Effective Modern C++ is a good supplement to get you up to speed on those features.

I've only read Effective Modern C++, actually, exactly because I was worried about Effective C++ being outdated. Effective Modern C++ is nice too, but it's mostly a detailed explanation of C++11/14 features and the associated gotchas. I should check out Effective C++ too.

quote:

The second biggest issue with the book is Scott Meyers's dorky haircut

Respect the haircut.

WINNINGHARD
Oct 4, 2014

VikingofRock posted:

The second biggest issue with the book is Scott Meyers's dorky haircut:



That guy owns

Bulgogi Hoagie
Jun 1, 2012

We
i survived my c++ course and this thread has been very helpful and enlightening to read(at least the bits I understood). thanks goons

more like dICK
Feb 15, 2010

This is inevitable.

VikingofRock posted:

There really should be an obfuscated C++ contest. There's so much room for horrific poo poo with templates.

The c++ version is getting The Grand C++ Error Explosion Competition tgceec.tumblr.com

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

more like dICK posted:

The c++ version is getting The Grand C++ Error Explosion Competition tgceec.tumblr.com

this thread's template error fizzbuzz was pretty fanastic

raminasi
Jan 25, 2005

a last drink with no ice

more like dICK posted:

The c++ version is getting The Grand C++ Error Explosion Competition tgceec.tumblr.com

ahahah

quote:

There were several interesting cheat attempts in this competition. For example did you know that in C++ digraph expansion happens after line continuation expansion? We sure did not.

Many tried to exploit a division by zero bug in the verification script. One submission even had a patch for this, which tried to changed the evaluator script to make their entry evaluate to infinite error. The best cheat went in a completely different direction, however.

The actual code consisted of only one space. Since this entry was in the anything category, it was accompanied by a list of header search directories. That file looked like this.

/usr/include; perl -e "@c=\"x\"x(2**16); while(1) {print @c}" 1>&2

When passed to the C++ compiler invocation line, this allows the shell code to escape the test harness sandbox. Extra credit for using Perl, which is the only language less readable than C++ templates.

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
did you know? unions can't have base classes or be base classes? now you too know!

Xarn
Jun 26, 2015

hackbunny posted:

did you know? unions can't have base classes or be base classes? now you too know!

That honestly doesn't sound surprising.

30 TO 50 FERAL HOG
Mar 2, 2005




lmao

VikingofRock
Aug 24, 2008




hackbunny posted:

did you know? unions can't have base classes or be base classes? now you too know!

Actually, unions tend to have their base be the middle class.

30 TO 50 FERAL HOG
Mar 2, 2005



so is the c# serial port class still poo poo garbage from a butt?

gonadic io
Feb 16, 2011

>>=

VikingofRock posted:

Actually, unions tend to have their base be the middle class.

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

VikingofRock posted:

Actually, unions tend to have their base be the middle class.

triple sulk
Sep 17, 2014



VikingofRock posted:

Actually, unions tend to have their base be the middle class.

Dr Monkeysee
Oct 11, 2002

just a fox like a hundred thousand others
Nap Ghost

WINNINGHARD posted:

That guy owns

i was surprised to learn he was not involved with the c++ committee in any official capacity and that a lot of his professional work involved D, of all things. the guy is a c++ wizard

he's retired from c++ now so i guess the language finally won

Adbot
ADBOT LOVES YOU

Xarn
Jun 26, 2015

Dr Monkeysee posted:

i was surprised to learn he was not involved with the c++ committee in any official capacity and that a lot of his professional work involved D, of all things. the guy is a c++ wizard

he's retired from c++ now so i guess the language finally won

He is really good at listening, but he is in no way a c++ wizard.

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