• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

All languages are allowed here! This is the catchall and there are several Java programmers that post in it.

Ok, thanks! And yes this is help with a course of mine. I seem to be having a hard time with my Radius formula. I will post everything from the problem along with the code I have written.

//Implement a class Balloon that models a spherical balloon that is being filled with air.
//The constructor constructs an empty balloon (That is, the volume is 0).

//Supply these methods:
// void addAir(double amount) adds the given amount of air
// double getVolume() gets the current volume
// double getSurfaceArea() gets the current surface area
// double getRadius() gets the current radius

/**
* models a spherical balloon that is being filled with air
*/
public class Balloon
{
private double volume;
double surfaceArea;
double radius;

/**
* Constructor for objects of class Balloon
*/
public Balloon()
{
volume = 0.0;
}
void addAir(double amount)
{
volume = volume + amount;
}
/**
* Gets the radius of the Balloon
*
* @return the radius of this balloon
*/
public double getRadius()
{
radius = Math.pow(3*volume/4*Math.PI, 1/3);
return radius;
}
/**
* Gets the volume of the Balloon
*
* @return the volume of this balloon
*/
public double getVolume()
{
return volume;
}
/**
* Gets the surface area of the Balloon
*
* @return the surfaceArea of this balloon
*/
public double getSurfaceArea()
{
surfaceArea = 4* Math.PI * Math.pow(radius,2);
return surfaceArea;
}


}
 

Rush_Khan

Member
I've never used Java and I may be wrong in this, but I'm guessing you need brackets to contain the (4*Math.PI), otherwise you're dividing by 4 and then multiplying the whole thing by pi.
 
Two things wrong with this line.

Code:
radius = Math.pow(3*volume/4*Math.PI, 1/3);

First, 1/3 is evaluated using integer math. The result of 1/3 is 0 and some number to the power of 0 is 1.

Second, know your order of operations, as Rush_Khan mentioned.
 
Two things wrong with this line.

Code:
radius = Math.pow(3*volume/4*Math.PI, 1/3);

First, 1/3 is evaluated using integer math. The result of 1/3 is 0 and some number to the power of 0 is 1.

Second, know your order of operations, as Rush_Khan mentioned.

radius = Math.pow(3*volume/(4*Math.PI), 1.0/3.0);
Thanks :'( I am dumb and didn't even look it over.
 

Massa

Member
Hey everyone. Does anyone have a good source with the popularity of programming languages over the years? I plan to use that info for a presentation I'm supposed to give soon. Doesn't have to be fully accurate (which would be impossible).
 
In a single cycle data path, there is a critical path. That is, the "longest" route in time will determine how long the enter path must take.

In a multicycle data path, everything is broken into stages. The longest stage determines how long each stage will have to be.

In a pipelined data path, it's the same as multicycle. In the PLDP, you can't skip stages (ie a J instr takes 3 stages in MCDP in comparison to PLDP), each instr must go through each stage in the pipeline. This is to keep everything synchronized in the pipeline.

So, you look at the how many cycles each TYPE of instr takes, and their percentage in an instr set. From there, you can calculate the average CPI, or cycles per instruction. (Cycles/Instr)

Clock rate is how many cycles can be done per second (Cycles/Second). The fast the clock rate, the more Cycles can be done per second, which means it will get through those instructions faster, as instructions are made up of multiple cycles.

Clock time is the amount of time it will take a processor to complete a set of instructions. This is just in seconds. It's the Clock Cycle Time * CPU Clock Cycles, or CPU Clock Cycles / Clock Rate (which makes sense, because it's the total number of clock cycles it needs to do, divided by how many it can do in a second).

Using these tools and the information given in problems, and doing a lot of unit matching, you should be able to solve practice problems easily.

Not sure about latency, I'm assuming you just compare the performance between two machines and that should be enough. You can do that just by comparing them as ratios.

Hope this helps, I have my final on it tomorrow too. Good luck dude!

Thanks. Good luck on your final.
 

mercviper

Member
So, for my senior project in the fall I plan on porting the board game telestrations over to a mobile platform. Probably Java/Android since I've been having trouble setting up a Mac VM on my pc. The only problem with this is my mobile devices are ios so live testing will be cumbersome for me lol. I've also never written a mobile app so I'm relying on past Java/C++ experience to pull me through.

Also, I want to incorporate network play via Internet so I think I need an application server, but I've never set one up before. Anyone know what I should look at to get started on this? I do have web server hosting already so I have access to a MySQL database and I've written web apps before, is this much different? (I.e. Establish a db connection and then use built in functions to interact with the db through the connection resource)
 

maeh2k

Member
So, for my senior project in the fall I plan on porting the board game telestrations over to a mobile platform. Probably Java/Android since I've been having trouble setting up a Mac VM on my pc. The only problem with this is my mobile devices are ios so live testing will be cumbersome for me lol. I've also never written a mobile app so I'm relying on past Java/C++ experience to pull me through.

Also, I want to incorporate network play via Internet so I think I need an application server, but I've never set one up before. Anyone know what I should look at to get started on this? I do have web server hosting already so I have access to a MySQL database and I've written web apps before, is this much different? (I.e. Establish a db connection and then use built in functions to interact with the db through the connection resource)

You could look into Xamarin. Free for students and you get an app that runs on iOS, Android, and Windows. You can build the app in visual studio. No mac necessary. http://xamarin.com/student
 

maeh2k

Member
In C#, if modulo is computationally expensive, what's an alternative to determine if an int is even or odd?

Unless you are working with absolutely giant matrices, I wouldn't worry about the performance of that basic an operation. You'll probably be fine.

Technically, for integers, one could use bit operations to specifically check the least significant bit. However, I'm not sure if that would really be any faster -- especially in C#.
 
Is there a TLDR version of why Rust is so good for someone that hasn't followed it at all?
It's a language that compiles to native code, is close in speed to C/C++, is much safer due to a powerful type system (without using a GC) and a lot of modern language features. Here is the official site.
In C#, if modulo is computationally expensive, what's an alternative to determine if an int is even or odd?
I don't think I've ever encountered a situation where a modulo operation was the bottleneck, but you can check the last bit to see if the number is even or odd. You'd definitely have to benchmark that, also.
 

Chris R

Member
I was wondering so I benchmarked it.

100 million random ints, modulo and bit checking.

bit checking took 1.4 seconds, modulo took 1.5 seconds.
 

wolfmat

Confirmed Asshole
In C#, if modulo is computationally expensive, what's an alternative to determine if an int is even or odd?

It's reasonable to assume that modulo is the fastest way to find out if an integer is even or odd. All other variants I can come up with need multiplication / division, multiple comparisons, the ampersand operator, multiple bitshifts, ...

Even Math.DivRem will lose because it respects the quotient.

I suppose (x & 1) == 0 will come close, but there's an extra comparison there, so it'll probably lose.

I would strongly advise using modulo.

rhfb said:
I was wondering so I benchmarked it.

100 million random ints, modulo and bit checking.

bit checking took 1.4 seconds, modulo took 1.5 seconds.
You'd really need to respect endianness there though, so that'll introduce more ops, which will make it lose.
 

maeh2k

Member
Thanks for the recommendation. This will be a good way for me to learn c# too, though from what I've seen of code snippets it never looked very different from Java.

As a student, you also get to use jetbrain's Resharper for free. If you set it to use IntelliJ Idea short-cuts, you can learn C#, IntelliJ Idea, and Xamarin at the same time :)
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
It's a language that compiles to native code, is close in speed to C/C++, is much safer due to a powerful type system (without using a GC) and a lot of modern language features. Here is the official site.

Thanks for the synopsis. Taking on C/C++ is a hell of a mighty task!
 

tokkun

Member
I was wondering so I benchmarked it.

100 million random ints, modulo and bit checking.

bit checking took 1.4 seconds, modulo took 1.5 seconds.

I tried it in C++, and I couldn't get GCC 4.9 to compile the modulo operation to a division instruction, even when using -O0. When I looked at the assembly, it ended up using shift + arithmetic.

Logical AND: 4.9 cycles per op
"Modulo": 6.6 cycles per op

So Logical AND was about 25% faster. However, I wrote the benchmark very carefully to minimize branch mispredictions and cache misses. In practice, they will almost certainly dominate.
 
It's reasonable to assume that modulo is the fastest way to find out if an integer is even or odd. All other variants I can come up with need multiplication / division, multiple comparisons, the ampersand operator, multiple bitshifts, ...

Even Math.DivRem will lose because it respects the quotient.

I suppose (x & 1) == 0 will come close, but there's an extra comparison there, so it'll probably lose.

I would strongly advise using modulo.


You'd really need to respect endianness there though, so that'll introduce more ops, which will make it lose.

Okay, thanks for the explanation. I'll inspect other parts of the code.
 

survivor

Banned
Anyone has experience working in embedded development or programming drivers or real time OS? Just curious how challenging is that sort of work compared to other areas.
 

Ambitious

Member
Still haven't found a solution to the problem I posted about on the last page.

I think I should describe it at a higher level:
It's a Java EE application. There are three classes involved with the part I'm working on right now: A Message-Driven Bean called Server, and two clients called Scheduler and Client. They communicate asynchronously using JMS queues. Here's the message flow:


  • The scheduler sends a message with some data to the server
  • Based on that data, the server creates a new entity, sets some fields, and persists it
  • The server creates a DTO from the previously persisted entity and sends it to the client
  • The client processes the data and sends the modified DTO back to the server
  • Using the ID from the received DTO, the server reads the corresponding entity from the database and updates it with the other fields from the DTO

The server is using a PersistenceContext powered by Hibernate and an embedded H2 database. So I'm persisting and reading using em.find(myEntity) and em.read(MyEntity.class, dto.getID()).

The first four steps work perfectly, but I have problems reading the entities from the DB. When I use the EntityManagers find() method with the ID, it doesn't find anything and just returns null. Yet, when I create a query that's just "from MyEntity where id=:id", it works. And after executing that query, the find() method suddenly returns the entity too.
This sounds like 1) the persist operation is not immediately committed and 2) reading from the table kinda triggers the commit. But why only if I use a query, and not if I use find()? I've printed the generated SQL, it's exactly the same for find() and createQuery().

However, sometimes (rarely), even createQuery doesn't find the entity. So using it as a workaround is no option.
I don't know what I'm missing here, but it has to be something regarding synchronization and/or transactions.
 

wolfmat

Confirmed Asshole
Still haven't found a solution to the problem I posted about on the last page.

I think I should describe it at a higher level:
It's a Java EE application. There are three classes involved with the part I'm working on right now: A Message-Driven Bean called Server, and two clients called Scheduler and Client. They communicate asynchronously using JMS queues. Here's the message flow:


  • The scheduler sends a message with some data to the server
  • Based on that data, the server creates a new entity, sets some fields, and persists it
  • The server creates a DTO from the previously persisted entity and sends it to the client
  • The client processes the data and sends the modified DTO back to the server
  • Using the ID from the received DTO, the server reads the corresponding entity from the database and updates it with the other fields from the DTO

The server is using a PersistenceContext powered by Hibernate and an embedded H2 database. So I'm persisting and reading using em.find(myEntity) and em.read(MyEntity.class, dto.getID()).

The first four steps work perfectly, but I have problems reading the entities from the DB. When I use the EntityManagers find() method with the ID, it doesn't find anything and just returns null. Yet, when I create a query that's just "from MyEntity where id=:id", it works. And after executing that query, the find() method suddenly returns the entity too.
This sounds like 1) the persist operation is not immediately committed and 2) reading from the table kinda triggers the commit. But why only if I use a query, and not if I use find()? I've printed the generated SQL, it's exactly the same for find() and createQuery().

However, sometimes (rarely), even createQuery doesn't find the entity. So using it as a workaround is no option.
I don't know what I'm missing here, but it has to be something regarding synchronization and/or transactions.

Check if the data in the row to be found is actually valid in every column -- maybe you've edited by hand and entered invalid data. Also check if the PK field is padded with whitespace or something for weird reasons.
 

msv

Member
Anyone here knowledgeable about async messaging in C#? Preferably ZMQ.

I'm trying to set up a completely multithreaded async client and server with ZMQ, as you would with JMS for example. Problem is now on the server side - I don't see how it's possible to make sure the asynchronous reply is sent to the proper client. My thought was to set up a ROUTER > PULL & PUSH sockets, so that the receive and reply can be in separate threads. I've read that having ROUTER > DEALER would be the right case for this, but I don't see how this can be truly async - you need to remain in the same thread in order to reply.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Haha that's great.

Man, this internship is great, but my time there has revolved around learning their tools. Feels like we should have had a version control/build bot class in school because I've known none of the stuff they've thrown at me.

A thousand times yes. And debugging too with actual debuggers.
 

survivor

Banned
Spent some time today working with C++ in Xcode. And it's such a pain in the ass, I'm this close to just installing Windows on my laptop to get Visual Studio.

Gonna try to force myself to use it some more over the weekend. Maybe I will somehow end up getting used to it.
 

upandaway

Member
Spent some time today working with C++ in Xcode. And it's such a pain in the ass, I'm this close to just installing Windows on my laptop to get Visual Studio.

Gonna try to force myself to use it some more over the weekend. Maybe I will somehow end up getting used to it.
Did you try out CLion? I tried that after getting annoyed with Xcode, and with booting Windows every time, ended up trying CLion out and it's been nice.
I only use it for C though so I don't know how good it is for C++, also needs a student license
 

mercviper

Member
Grr, don't even get me started. It angers me they don't teach basic debugging skills in school. It's so easy to do and will increase your grades a ton
Is there more to debugging than reading the compiler errors and stepping in/out/over through your program run? Because that's how I debug my stuff. That and writing output at certain steps to fix logic errors. I find compiler error readings to be the most helpful and it baffles me that so many students don't understand them.
 
Is there more to debugging than reading the compiler errors and stepping in/out/over through your program run? Because that's how I debug my stuff. That and writing output at certain steps to fix logic errors. I find compiler error readings to be the most helpful and it baffles me that so many students don't understand them.
While it's basically what you described, learning the debugger's commands and using them effectively is seriously helping me. Learning how to do something as simple as jumping between threads, inspecting data at different frames, and putting a watch point on variables really ups their value.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Is there more to debugging than reading the compiler errors and stepping in/out/over through your program run? Because that's how I debug my stuff. That and writing output at certain steps to fix logic errors. I find compiler error readings to be the most helpful and it baffles me that so many students don't understand them.

You'd be scared to know how many students get by through classes without even that much knowledge of debugging.
 

Jokab

Member
You'd be scared to know how many students get by through classes without even that much knowledge of debugging.

I'm finishing up my bachelor's thesis and out of the five other members in the group, exactly one knew how to use the debugger before I taught them. They're all comp sci students.
 

survivor

Banned
Did you try out CLion? I tried that after getting annoyed with Xcode, and with booting Windows every time, ended up trying CLion out and it's been nice.
I only use it for C though so I don't know how good it is for C++, also needs a student license
Just tried it this morning. Had to mess around a bit with CMake to get it to work with SDL2. I'm a bit unsure how the building process works, but at least for now I got it running after looking up some sample build steps online.

Will probably stick with it for now.
 

mvtn

Member
Hello programmers!

First time here, what's up?

I've been digging some Go lately, using Vim primarily for good 5-6 months.
 
Is there more to debugging than reading the compiler errors and stepping in/out/over through your program run? Because that's how I debug my stuff. That and writing output at certain steps to fix logic errors. I find compiler error readings to be the most helpful and it baffles me that so many students don't understand them.

At the most basic level, no that's what it is. But there are lots of advanced things you do. Like conditional breakpoints, data breakpoints, calling functions in your program from the debugger, inspecting/manipulating memory of the target process, tracepoints, set next statement to move the instruction pointer somewhere else, suspending/resuming threads from inside the debugger. Could probably come up with more too.
 

poweld

Member
Hello programmers!

First time here, what's up?

I've been digging some Go lately, using Vim primarily for good 5-6 months.

Sweet, welcome. If you have any questions about either there are a bunch of clever people here that can help.

I've been using vim for about 8 years and did a project a few months back in golang.
 
Just tried it this morning. Had to mess around a bit with CMake to get it to work with SDL2. I'm a bit unsure how the building process works, but at least for now I got it running after looking up some sample build steps online.

Will probably stick with it for now.

I quite like CMake. The syntax is a little weird, but it requires very little configuration for simple projects, which I really like.

How can I find out the "loudest" frequency in an audio signal? I want to be able to hit a key on a piano and the program should tell me what the base frequency of that note is. My first idea was to go with a Fourier transform and find the frequency with the highest amplitude in there. Theoretically, that sounds easy, but I'm having some trouble implementing that in Java because I know next to nothing about audio processing. Right now I'm using Java with Apache Commons Math and javax.audio for the audio input via microphone. Here is the code I have for now. I've tested it with 440 Hz and 880 Hz and the value it puts out for 880 Hz is actually lower than the value for 440 Hz, so I must be doing something pretty wrong. Oh, and for now I'm also only doing the processing on a pretty small window (the buffer size is 32k, one sample is 2 bytes, so it's about 0.75 seconds with 44.1 kHz), not sure how that impacts things.
 

Juanfp

Member
Can someone recommend me a website or book to learn to code in python?
I have some experience using matlab, I took 2 course that require to use it in my university, but now I want to learn by my self a proper programming language.
 
Top Bottom