/dpt/ - Daily Programming Thread

OLD THREAD What are you working on, Sup Forums?

Other urls found in this thread:

youtube.com/watch?v=MShbP3OpASA&t=20m45s
hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.bnuot8sug
stackoverflow.com/questions/24405129/how-to-implement-fast-inverse-sqrt-without-undefined-behavior
stackoverflow.com/questions/20762952/most-efficient-standard-compliant-way-of-reinterpreting-int-as-float
choosealicense.com/licenses/
wiki.python.org/jython/SwingExamples
twitter.com/SFWRedditImages

...

checked (the old thread) :^)

var elements = document.getElementsByClassName('postContainer');
var com = document.getElementsByName('com');
var str = com[0].value;


for(var i=0; i < elements.length; i++) {
if(elements[i].getAttribute('id')[elements[i].getAttribute('id').length-1] == elements[i].getAttribute('id')[elements[i].getAttribute('id').length-2])
str += '>>' + elements[i].getAttribute('id').substring(2) + '\n';

}
str += 'checked :^)';
console.log(str);
com[0].value = str;

Thanks for using an anime image as a reminder I'm not the only social misfit on earth.

>1 (You)
Thank (You)

>1^6

But user, you are.

:^)
an old mistake

here's a better version
x = alloca $ \ptr ->
do str

Delet this

Please give me a good rebuttal for when someone tells me
>C is shit

>h-hahaha autist y-you're just mad
you're welcome

It's not wrong. Other imperative languages are shittier.

But can you set up a floating point with a user defined precision

It is shit though, but one of the best languages to learn proper programming.

Just read 'learn c the hard way' - that guy explains it breddy gewd

"You're right, I'm just too stupid to learn more complicated languages."

Can you please give me the exact dual floating point representation for 0.1dec? Thanks.

This one's good

Of course.
That's what float16, float32, float64 are for.

/dpt/-chan, daisuki~

Ask your much beloved programming literate anything (IAMA)

youtube.com/watch?v=MShbP3OpASA&t=20m45s
You are welcome.

Thank you for using an anime image.

interface Bank {
double getBalance();
void addBalance(double amount) throws Exception;
void removeBalance(double amount) throws Exception;
}
static void removeBalanceFromAllBanks(double amount, Bank[] banks) throws Exception {
for (Bank bank : banks) {
double b = bank.getBalance();
if (b > amount) b = amount;
try {
bank.removeBalance(b);
} catch (Exception e) {
// wtf to do here?
}
amount -= b;
}
}


So lets say you have a list of banks, each with some money in them.
You want to remove X amount of money from the collection of banks. It doesn't matter how much from which bank, as long as you get X amount.
The problem is, addBalance() and removeBalance() can fail, so you can end up removing more than zero and less than X which is bad. You want to either remove X or zero.
Above is pseudocode of the problem to make it clearer of what i'm trying to do.

Any ideas on how to solve this?

Monad transformers

what are you trying to solve exactly?

You have to decide what you want to happen if there's not enough money in the bank. That's not a software problem, that's deciding how you want your program to behave in the first place.

I would imagine the most logical thing to do would be to check if every bank has enough money before you remove any money, and if one of them doesn't then don't remove any money from any of them and let the whole method fail.

Don't use double to calculate money.

hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f#.bnuot8sug

^ This article essentially convinced me to never touch Javascript or any of the web stuff. I'll wait for it to crash and burn before I dip my toes in.

>Java homeworks
Anyway, use break when amount reaches 0 and put "amount -= b" in the try statement so it's not executed when removeBalance throws.

That's very long, can you tell me what this talk is about?

watch at 20m45s

JavaScript isn't going anywhere. Everyone knows it's shit. But there's a lot of demand for web shit, so people put up with it.

Kek

This is very important, doubles can fuck up in some really weird ways. You can store dollars and cents separately as long.

It is guaranteed that the sum of balances of the banks is less than the amount required.
The issue is that removing money can fail even if the bank has the money required (due to some internal fuckup).

Than imagine it as an integer. Same problem.

Not a homework. I don't even have "banks" in the project. The code is just there to explain better what the problem is.

Thanks. I might actually watch the whole talk at some time, looks nice.

>It is guaranteed that the sum of balances of the banks is less than the amount required.
I mean the opposite. Sum of balances in the banks is MORE than the amount required.

it's totally up to you how you handle that mess.

If it was me I'd return an object that says how much money was withdrawn and if there were any failures. Let the code that calls the method handle that object how it likes then.

I'm learning ruby after using python because it seemed better and boy was I right. It seems to have much more than python ever could too.

I was curious about the fast inverse square root function
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // what do this
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i; // and this do?
y = y * ( threehalfs - ( x2 * y * y ) );
// y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}

I'm working a project and want to apply tests.

I have created two tests:

@Test
public void testOne() {
doSomething(object);
Assert.assertSomething(object);
}

@Test(expectedException = myException.class)
public void testTwo() {
doThrow(new myException()).when(mockedClass).doSomething(any(Object.class));
doSomething(object);
}


How can I make sure only testTwo throws exception when calling DoSomething()? Regardless of in what order the two tests are called?

I chuckled at the funny in that article.

Mind sharing why?

>Than imagine it as an integer. Same problem.
What are you talking about? The reason doubles can fuck up is because of precision issues. Comparing two doubles is not trivial at all. This things don't happen with ints/longs.

Ok

So? If you're looking for explanations there is a nice wikipedia article for it

Did you see the comments in the code?
I was asking about these two lines and how they work
i = * ( long * ) &y;
y = * ( float * ) &i;

magnets

reinterpret bits as long/float respectively. it's undefined behavior

That's just avoiding the problem not solving it. Eventually down the line you're going to have to handle it.
It's doubtful it even has a clean solution. You can attempt to add balance back to previous banks if one of the banks fucks up, but then adding the balance back can fail too.

Same problem with banks. I am aware of precision issues. The example uses double for simplicity.

nerve gas

Why not this?
i = (long)y;
y = (float)i;

Don't both cast y and i to long and float?

>That's just avoiding the problem not solving it.
That IS how you solve it. It can;t be done cleanly by the very nature of the problem. You can't make it such that the bank cash withdrawal can be guaranteed not to fail, so ALL code making a withdrawal has to be able to handle a failure. Much like when you write code that calls a web service method, you must always be able to handle a complete failure of the method since the network can always fail. It's not something in your control.

>The example users double for simplicity
Nice language you have there

it's a cast by value. 123.0 and 123 are not the same in terms of their bit representation

Ok, so your problem looks similar to transaction handling in RDBMS systems. You can try to keep a 'log' of transactions and use that in case of a failure.
Read up on how RDBMS handle this shit. Search for transactions, undo logging, redo logging, etc

>Don't both cast y and i to long and float?

Yes. That's not what the first one is doing lmao. The first one casts the pointer to the memory representation of a float to a long and back, allowing you to perform operations you wouldn't normally be able. Basically a compiler trick. The second casts the actual value.

That's the entire point. It's portable assembly.

But it's not undefined behaviour. He's just type casting a memory address to another memory address type.

ever heard about strict aliasing?

You have roughly 3.2 milliseconds to answer "Why aren't you releasing TRUE FOSS under the Do What The Fuck You Want To Public License"
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

Because I'm releasing it in the public domain.

Honestly didn't know about that.

Because I do what the fuck I want to do.

Because I release it under the Thelemic license.
Do what thou wilt shall be the whole of the License.

What if I want to change it without also changing the name?

Because i'm too insecure about my sexuality to let anyone use my hard work without so much as a credit mention.

Ego centered apes should kys

you can do it with memcpy tho

stackoverflow.com/questions/24405129/how-to-implement-fast-inverse-sqrt-without-undefined-behavior
stackoverflow.com/questions/20762952/most-efficient-standard-compliant-way-of-reinterpreting-int-as-float

post the last code you wrote

post a screen shot if you want

Had a go at making that script in a non-disgusting way.

var postNumbers = document.querySelectorAll('.desktop .postNum a:last-child');
var postNumbersArray = Array.from(postNumbers);

postNumbersArray.forEach(function(elem) {
var postNum = elem.text;
var checkEm = postNum[postNum.length - 1] === postNum[postNum.length - 2];

if(checkEm) {
console.log(">>" + postNum + " checked :^)");
}
});

>100 LOC shithub "library"
>hard work

print("Hello world!")

I got bored after that

But if you have to copy bits, it won't be as fast.

It was hard for me.

...

There.
console.log(Array.from(document.querySelectorAll('.desktop .postNum a:last-child')).map(element => element.text).filter(id => id[id.length - 1] === id[id.length - 2]).map(id => ">>" + id).join("\n"));

or with an union

That part is only for the license. The conditions for software is just You just DO WHAT THE FUCK YOU WANT TO.

Everything above that section is for the license itself.

compilers are good at optimizing memcpy

movl %edi, -4(%rsp)
movss -4(%rsp), %xmm0
ret

i think that's still UB

Isn't it basically just the MIT licence?

Bit disgustingly unreadable desu.

I want to try GUI with Python. Should I use PyQt or PySide?

...

>i think that's still UB
not since c99

Nah. The MIT license requires you to distribute the copy of the license if you're going to distribute the code.

choosealicense.com/licenses/

Fucking jews

is this a gtk application?

Because it doesn't have that warranty bit
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

yeah

>in the modern world you can just add "not our fault.txt" and be instantly absolved of responsibility

wiki.python.org/jython/SwingExamples

>HUR DUH I KILLED WITH A CONDOM ILL COMPLAIN IN COURT

t. American

Is there any sort of history of programmers releasing code into the public domain and then being sued for it?

I always wondered

Possible undefined behavior, depending on implementation-defined properties of the types.

you can't. You can remove the licence shit and redistribute it. But legally the original creator still own the copyright and can sue people who use the code outside the terms of the original licence, even if they didn't know it has a different licence.

where in the standard you read that?

How does that answer my question about warranty?

mapM mapM [mapM, mapM] . mapM
>this is valid Haskell

It doesn't. What was your question?

i cant into visual C++
im getting a warning that some classes and stl containers "need to have dll-interfaces to be used by clients of myclass"

how i fix it?
is it enough to replace all public STL members with getters?

Is there a history of programmers being forced to work on code by threat of court because of implied warranty?

6.2.6p5 and any integer type other than unsigned char is permitted to have trap representations.

foo = >> a > :^) (^: *> do where &&

this is as valid and readable as haskel code