• 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

Granadier

Is currently on Stage 1: Denial regarding the service game future
Don't know if this is right thread to ask, I've been unemployed for a while, the job I had previously was in research and development in material science. I want to switch in to a career that allows me to work close to new york city, which material science doesn't allow. So on the advice of a web developer friend, I've been trying to learn Ruby on Rails as that's the new hot shit and there seems to be a lot of jobs compared to the few material science jobs that are all in the middle of no where Penn.

I know with Ruby on Rails for web development you are expected to know the entire vertical stack. I am trying to get a grasp at the type of competency i would need to get an entry level job, and would a masters in comp sci help out?

I would love if any of you guys can point me to an example web app written with ruby on git hub so i can get a grasp the type of knowledge i should have for a fully deployed site.

My current experience with Ruby on Rails is, hey gems are fucking cool you can do so much with them, to wait there's no IDE, wait there's no hand holding? Configuring your gem packages is a pain in the ass? and there's a million versions of these Gems?

First of all, RubyMine is an IDE for Ruby on Rails.

It seems like you are wanting to jumpstart into web-dev by going straight to Rails, but I think you really should start by getting a solid understanding of the full-stack, (HTML/CSS/Javascript). You won't be of much use if you don't understand what you're trying to interact with through Rails if you don't understand those core fundamentals.

Also from what I understand, Ruby is really loud right now because it's new and seems really awesome at first. PHP is still by far the dominant player in the web world though currently. Rails expertise may make you more desirable for people looking at building a new project or site, but PHP expertise would make you more desirable to people that have to maintain established systems. Here's a nice article on the topic.

Just my 2¢. I'm going through a similar experience currently because I want to get experience with front-end development, and every time I try to overextend beyond what I actually know to get something done quick it just turns into a clusterfuck.
 
First of all, RubyMine is an IDE for Ruby on Rails.

It seems like you are wanting to jumpstart into web-dev by going straight to Rails, but I think you really should start by getting a solid understanding of the full-stack, (HTML/CSS/Javascript). You won't be of much use if you don't understand what you're trying to interact with through Rails if you don't understand those core fundamentals.

Also from what I understand, Ruby is really loud right now because it's new and seems really awesome at first. PHP is still by far the dominant player in the web world though currently. Rails expertise may make you more desirable for people looking at building a new project or site, but PHP expertise would make you more desirable to people that have to maintain established systems. Here's a nice article on the topic.

Just my 2¢. I'm going through a similar experience currently because I want to get experience with front-end development, and every time I try to overextend beyond what I actually know to get something done quick it just turns into a clusterfuck.

I think i'm going to stick with ruby on rails, the front end stuff I have to learn anyways if i want to make something more then a basic website. But Ruby on Rails already generates everything i need to run a website i think it's a good way to learn MVC architecture and Application development through testing. The other way is through ASP.NET MVC development and then I have to learn C# just when i'm trying to get my head around what I can generate with Ruby on Rails/Gems framework.

What's the difference between learning Ruby and PHP both are scripting languages right? So one should move over to the other?

I'll check out RubyMine, it was not suggested in the Ruby Tutorial website, i've been using Textmate and terminal for all the debugging which is not fun at all when I get an error.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I think i'm going to stick with ruby on rails, the front end stuff I have to learn anyways if i want to make something more then a basic website. But Ruby on Rails already generates everything i need to run a website i think it's a good way to learn MVC architecture and Application development through testing. The other way is through ASP.NET MVC development and then I have to learn C# just when i'm trying to get my head around what I can generate with Ruby on Rails/Gems framework.

What's the difference between learning Ruby and PHP both are scripting languages right? So one should move over to the other?

I'll check out RubyMine, it was not suggested in the Ruby Tutorial website, i've been using Textmate and terminal for all the debugging which is not fun at all when I get an error.

PHP probably won't move aside for Ruby for quite some time. Just because there is sooooooo much of the web that relies on PHP. If it were me I would learn Rails over PHP as well.

I haven't had experience directly with RubyMine, but it is made by JetBrains, the creators of IntelliJ, and they have extremely solid IDE's. I use their IntelliJ IDEA for Java development and their Webstorm for my web-dev stuff. They also have PHPStorm if you do decide to look into that side.

Unfortunately beyond those answers I can't be much more help. I don't have experience with writing either PHP or Ruby beyond the basic "Hello World" programs. Maybe someone else in here will be able to answer your more technical questions better.
 

BreakyBoy

o_O @_@ O_o
Don't know if this is right thread to ask, I've been unemployed for a while, the job I had previously was in research and development in material science. I want to switch in to a career that allows me to work close to new york city, which material science doesn't allow. So on the advice of a web developer friend, I've been trying to learn Ruby on Rails as that's the new hot shit and there seems to be a lot of jobs compared to the few material science jobs that are all in the middle of no where Penn.

[snip]

I, and others, have written many long winded answers to the "which language do I use" question.

Not to belittle yours among the others, but here's the quick answer: Just pick one

Ruby is fine. If you have job prospects, more so.

As for Rails, IDEs, debugging, etc. Most Ruby developers that I know (and I include myself here), will tell you that they use a text editor and not much else. RubyMine is pretty good if you want a full IDE, otherwise, I'd recommend SublimeText and adding some automatic linters like RuboCop.

As already stated, if you don't know the HTML/CSS/JS stack already, that will be a pain point for you on your journey. Learning it as you go is definitely a viable option, but don't expect your layouts to look good/remotely modern until you get better at those. Or learn a framework like Bootstrap, and never stray from the template, and everything will look the same. Neat, but the same.

As for complementary skills that I would look for in a Rails developer, it's always nice if you're competent in administering a web server (Apache/Nginx are the usual suspects, typically running on some flavor of Linux) and/or a database (MySQL is fine for learning. What you get on the job will likely be an RDBMS of some sort, so the skills will generally transfer). Experience with some frameworks such as Bootstrap would also be helpful.

If you already have programming experience, look up RubyKoans to get up to speed on the quirks of the language in a quick way. Test-driven development (TDD) is also all the rage in Ruby/Rails shops, and I would suggest that it works pretty well as a way to start learning anyway. Speaking of which, Michael Hartl's Rails Tutorial ebook/site is excellent and will get you going quickly in TDD Rails.

That's enough of a brain dump at 3 AM. I'm supposed to be on vacation. Don't be afraid to ask questions specific to whatever you're working on. There's also a web-development specific OT thread that has regular activity that you can ask in. Good luck!
 
Thanks for the replys, I have been doing ruby koans and I have been doing the Ruby on Rails tutorial which does use bootstrap gem to generate the CSS. If you have time in the future can you list the typical gem packages used for development, as that would help me to focus on learning the few packages most real relevant for the job. For example respec and capybara are the two gems used for test scripting in the tutorial is this the "industry norm".
 
I have a somewhat industry related question for you...

A little over a year ago I moved from the Midwest US to a pretty good sized coastal city in the Southeastern US and accepted a Senior Software Engineer position with a smaller dev house (50~ employees.)

Prior to my move, my wife had worked for an insurance company and carried our insurance with fantastic benefits. Now that we've moved she stays at home with my preschool age daughter.

Anyway, I was shocked when I found out how much benefits cost. I think my base salary is competitive with others in the area with my job title and experience, but I'm paying about 12% in insurance. The company pays 100% for me, but 0% for my dependents.

Is this cost outlandish or am I off base here? The benefits are kind of shitty on top of the cost. I'm really tired of paying this and pretty sure I could get in with a bigger company in the area, but maybe I should readjust expectations if this is common practice...
 

John_B

Member
Hey guys, I need to calculate some ranks for a scoreboard.

I retrieve an array with players and their points in a random order. Now if two or more players share the same amount of points they also need to share the same rank, so sorting the array and using the key as the rank is not of much help.

The following is my simple approach, but it's seems a bit messy as the array also needs to be sorted by rank and name afterwards with even more logic.

Anyone with a nifty approach that also sorts the array in one go? Much appreciated.

Code:
<?php

// players array

$players = array(
    array(
        name => 'Player 1',
        points => 2,
        rank => 0,
    ),
    array(
        name => 'Player 2',
        points => 1,
        rank => 0,
    ),
    array(
        name => 'Player 3',
        points => 2,
        rank => 0,
    ),
    array(
        name => 'Player 4',
        points => 3,
        rank => 0,
    ),
); 

// rank function
// returns rank of one player against array of players

function rank($player, $opponents)
{
    $rank = 1;
    
    foreach ($opponents as $opponent)
    {
        if ($player['points'] < $opponent['points'])
        {
            $rank++;
        }
    }
    
    return $rank;
}

// loop through players array to assign ranks

foreach ($players as $key => $player)
{
    $players[$key]['rank'] = rank($player, $players);
}

var_dump($players);

?>
 
I'm not too familiar with php so there might be a better shorthand method for ranking array values by some arbitrary key but this approach will get you what you want.

First we sort the array of players by points in descending order, then iterate through the array and update the rank while adjusting the rank value when we detect a change in points. This also has the advantage of being nlogn in complexity as opposed to n^2 although it's unlikely to matter unless you're dealing with very large arrays of players.

edit: I just noticed you mentioned sorting by name as well, I just updated the comparison function here to sort by name in the event that points are the same. After the first sort and rank assignment you should be done.

Code:
// comparator function, if points are equal, sort by name,
// otherwise, high points come before low
function compare_players($a, $b) {
    if ($a['points'] === $b['points']) {
        return strcmp($a['name'], $b['name']);
    }
    return $b['points'] - $a['points'];
}

// sort players array by points descending then names ascending
usort($players, 'compare_players');

// assign each player a rank as their point score changes
// assuming ranks start from 1 and -1 is not a legal point value
$rank = 0;
$last_points = -1;
foreach ($players as $key => $player) {
    if ($last_points != $player['points']) {
        $rank++;
        $last_points = $player['points'];
    }
    $players[$key]['rank'] = $rank;
}
 

ss_lemonade

Member
Here's a question for PHP experts.

Is there a bug in PHP that prevents one from setting memory_limit to something high like 4096mb? I know it sounds like overkill, but we're creating a system that works with Craigslist's bulk posting API and in that system, it needs to post several hundred ads in one go. The way CL's api works is it accepts an XML file formatted with the necessary data, including images in base64 format.

Anyway, we were initially running into memory related issues and one of the devs here decided to bump memory_limit up to a large number like 4gig to see if it would help (the site is hosted on a 64bit server with 32gb of ram IIRC). The memory errors went away but now, a new problem would randomly occur where we would get a No Data Received (chrome) error, a white screen, or a connection reset error. Looking at the error_log, I see a lot of "child pid # exit signal Segmentation fault" and zend_mm_heap corrupted messages. I also get this error a lot in the domain-error.log file:

PHP Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 523800 bytes) in Unknown on line 0

Looks like a generic error to me, don't know where the 256kb memory limit is coming from. We lowered the memory_limit value for now and that problem seems to have gone away.
 

BreakyBoy

o_O @_@ O_o
Here's a question for PHP experts.

Is there a bug in PHP that prevents one from setting memory_limit to something high like 4096mb?

It's been a few years since I've worked with PHP, but:

There might just be a global restriction? Check any .htaccess files that might be affecting your site. Are you running anything that might be imposing safe limits, like Suhosin, or maybe some safe mode on the web server? Are you using a shared host? Dedicated? Private (i.e. rolled your own)?

How are you setting the new limit? Code snippet? What does your phpinfo() say about your memory limits?

And I'm assuming you have control over the full stack. That's not quite clear. Are you running on a private/dedicated host, or are you using shared hosting (which might have account limits for memory usage that you can't override directly)?
 

ss_lemonade

Member
It's been a few years since I've worked with PHP, but:

There might just be a global restriction? Check any .htaccess files that might be affecting your site. Are you running anything that might be imposing safe limits, like Suhosin, or maybe some safe mode on the web server? Are you using a shared host? Dedicated? Private (i.e. rolled your own)?

How are you setting the new limit? Code snippet? What does your phpinfo() say about your memory limits?

And I'm assuming you have control over the full stack. That's not quite clear. Are you running on a private/dedicated host, or are you using shared hosting (which might have account limits for memory usage that you can't override directly)?
Hi BreakyBoy, its a dedicated server over at rackspace. Suhosin is installed I believe but I don't think its imposing any limits for memory. The new limits are being set in the appropriate php.ini file and phpinfo does show the updated numbers. There is an htaccess file but there's no memory related settings being modified there.

I just found this old bug submission through google search:

https://bugs.php.net/bug.php?id=63831

Exactly the same problem and logged error we're getting but ours happens randomly. We are using 4096M as the memory_limit value though, so we'll try setting it in bytes later to see if it works better that way
 

BreakyBoy

o_O @_@ O_o
Hi BreakyBoy, its a dedicated server over at rackspace. Suhosin is installed I believe but I don't think its imposing any limits for memory. The new limits are being set in the appropriate php.ini file and phpinfo does show the updated numbers. There is an htaccess file but there's no memory related settings being modified there.

I just found this old bug submission through google search:

https://bugs.php.net/bug.php?id=63831

Exactly the same problem and logged error we're getting but ours happens randomly. We are using 4096M as the memory_limit value though, so we'll try setting it in bytes later to see if it works better that way

That links to another similar bug report where someone notes that the memory limit is stored in a long int, which means that when you expand 4096M out to bytes (4294967296), it ends up being a value out of the acceptable range of an unsigned long int (0-4294967295). And it's marked as not a bug, so now I'm curious if it will actually solve your problem.

Report back if you can. You might end up helping someone else out.
 

ss_lemonade

Member
That links to another similar bug report where someone notes that the memory limit is stored in a long int, which means that when you expand 4096M out to bytes (4294967296), it ends up being a value out of the acceptable range of an unsigned long int (0-4294967295). And it's marked as not a bug, so now I'm curious if it will actually solve your problem.

Report back if you can. You might end up helping someone else out.
Looks like we didn't have time to test this today. I did notice that linked issue but that shouldn't be the case on our server since we're on a 64bit one and should be able to handle values up to 9223372036854775807
 

Exuro

Member
Could someone tell my why this opengl2 example makes my window display whats behind it? As in the window opens up in front of my console and it shows what my console is showing, as if its transparent. If I resize the window it switches to display the triangle. Also if I change it to single buffer and switch the glutSwapBuffers() with glFlush it seems to work. Tried looking on google and couldn't find anything.

http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/
 

mooooose

Member
Hey all, I had a question. I'm working on an assigment for school and it uses JavaFX for it's GUI. I am having some problems with it. We were given code with a bunch of bugs, and we have to fix it. My professor wrote the code such that there are multiple Panes, one of the "Game" (Hangman), one for stats, and one for help. The "switcher" between these panes was broken, so the tab buttons didn't do anything. Now, the way I fixed it was by calling the pane inside the scene which is shown in the stage (a borderpane) and switching the center focus to whatever Pane was necessary, given a change in the UI state (denoted by an enum and passed as parameter to the method that changes the scene).

I don't want to post code because I could get in trouble with my Uni, but I was wondering, though this works, there appears to be some rendering issues.

Sometimes, the screens will go blank after they are clicked on. Like they refuse to render, or the text is there but requires to be highlighted before it's visible. Does anyone know why?
 

hrab

Member
I haven't done much in JavaFX, but every time when i've had similiar problems, invoking the code that was supposed to change something in GUI from Platform.runLater() was enough to solve it.
 

mooooose

Member
I think the problem comes from the fact that they are HTML files which are loaded into a JEditorPane. They are constantly fed information from the game and updated, and they either don't update quickly or just don't appear unless highlighted or something.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I'm playing Ruby Warrior.

Someone explain to me why this runs:
Code:
class Player
  @health = 20
  def play_turn(warrior)
    if warrior.feel.empty? 
      if warrior.health < 20 && warrior.health < @health
        warrior.rest!
      else
        warrior.walk!
      end
    else
      warrior.attack!
    end
    @health = warrior.health
  end
end

Whereas this gives me an error.
Code:
class Player
  @health = 20
  def play_turn(warrior)
    if warrior.feel.empty? && warrior.health < @health # The offending line
      if warrior.health < 20 
        warrior.rest!
      else
        warrior.walk!
      end
    else
      warrior.attack!
    end
    @health = warrior.health
  end
end
comparison of Fixnum with nil failed
Player.rb:5:in `<'
Player.rb:5:in `play_turn'
 
@health is an instance variable, you have to call it in play_turn(warrior)
you declared it in the class i think if you do it in class it has to be @@health
 

Haly

One day I realized that sadness is just another word for not enough coffee.
It works regardless of whether or not I define @health before play_turn(warrior).

The only difference between the two is that "&& warrior.health < @health" is moved to the previous conditional, both of which are inside play_turn(warrior).

EDIT: Okay, so I guess @health is nil when the method first starts running. The only reason the first one works is because its never reached before @health is set at the bottom, and they don't see why it's necessary to tell me that.

Scripting languages...
crazy.gif
crazy.gif
crazy.gif
 
Ruby on Rails developers make good money and there's alot of demand. There's no demand for my engineering major so i'm learning this to make money?
 

hateradio

The Most Dangerous Yes Man
Are you trying to make @health a constant?

you should use HEALTH = 20 instead.

Code:
class Player
  HEALTH = 20

// and later . . .

  warrior.health < Player::HEALTH

Otherwise you can just create it and initialize it.

Code:
  attr_reader :health

  def initialize
    @health = 20
  end

Is there a Python Warrior? It's even lazier and spells 'else if' as 'elif'. Plus I wouldn't have to worry about typing out all these 'end' lines or weird !/? method calls.
!/? aren't necessarily weird. They just notate when something is either going to mutate the object or cause some side effects, and in the case of ?, they're just mostly used for Boolean return methods.

The end thing is kind of annoying.
 
Are you trying to make @health a constant?
!/? aren't necessarily weird. They just notate when something is either going to mutate the object or cause some side effects, and in the case of ?, they're just mostly used for Boolean return methods.

Yeah this convention comes from Scheme I believe (maybe something else before it) and I really really wish it was a thing in Python, it's so much nicer to know when a method is going to mutate state especially since I always try to minimise mutation in everything I write.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Dear God Angular is amazing. Feels so much less hacky than JQuery does most of the time.

I feel the same way. Unfortunately the web app we are working on at work was built with jQuery, so we can't easily switch over entirely to AngularJS :/

Crossing my fingers that we can start a future project with Angular as a base, or else I'll just keep messing around with it on my own.

------------

edit for question:

Have any of you dealt with creating a GUI for a three.js application without utilizing dat.gui?
We are using dat.gui right now, but I want to transition to something more elegant.
 

r1chard

Member
Dear God Angular is amazing. Feels so much less hacky than JQuery does most of the time.
Yes, yes it is. Finally some structure in Javascript applications. You do run into the rare digest issue, and older IE compatibility can suck sometimes, if that's a corporate requirement. But oh, those namespaces. Be still my heart.

Yes, I'm deliberately ignoring anything the node.js programming language brings to the table.


It is a shame that the lovely dependency injection is all messed up by minimizers though :(
 

RELAYER

Banned
Does anyone have any experience with mbed?

It's a microcontroller, it runs on C.

I'm studying electrical engineering and I have to take this class but I don't have much experience programming and there are a lot of things I'm not understanding.

I'm doing a project that is fairly simple but I can't get it to work right.
I have 4 leds and they must fade on and off in a cycle, from the 1st to the 2nd to the 3rd to the 4th, and then back down again, over and over. On the mbed you use something called pulse-width modulation to make them fade, but I don't understand the intricacies of what is happening.

so far I did something like this -

Code:
PwmOut led1(LED1); //declaring the led as a pulse-width modulation object
float i;


int main() {
led1.period(0.001); //setting the frequency of the modulation of led1
   while(1) {  
      for (i = 0; i < 1.0; i = i + 0.05) {
      led1 = i; wait(0.1);
      }
   }
}

edit: My formatting keeps getting obliterated, sorry.


So what this code does is slowly bring the LED brighter and brighter until it is 100 percent. Now this is just for one LED. I'm going to have 3 more after it. But what I don't understand is how to make it so that, after the 1st LED completes its loop, I can make the 2nd LED begin its modulation, and have the 1st LED stay off.

I thought that simply writing the code sequentially, i.e. writing 4 different for loops would solve this. 1 for each LED. I figured the code would go through each for loop one at a time. This does not happen. Another problem is that the value of the wait function seems to affect the modulation of the LED. The higher the wait function, the slower the LED increases to maximum brightness. I don't know why this happens.

Does anyone know what I'm talking about or have any advice? I can try to explain more about what I'm doing if needed. I'm continuously feeling like a huge idiot this semester.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
Use
Code:
 tags to preserve whitespace.

[CODE]like
  so
 

arit

Member
It's a microcontroller, it runs on C.

Actually the mbed libraries you are using are written in c++ ;)

So what this code does is slowly bring the LED brighter and brighter until it is 100 percent. Now this is just for one LED. I'm going to have 3 more after it. But what I don't understand is how to make it so that, after the 1st LED completes its loop, I can make the 2nd LED begin its modulation, and have the 1st LED stay off.

I thought that simply writing the code sequentially, i.e. writing 4 different for loops would solve this. 1 for each LED. I figured the code would go through each for loop one at a time. This does not happen.

Should work. Did you turn the LED off after bringing it to 100% brighness and before starting the loop for the next LED?
And I suppose that every LED is connected to its own pin? In that case you'd have to create a seperate PwmOut Object for each of it (at least in your code you only did create one for LED1).

Another problem is that the value of the wait function seems to affect the modulation of the LED. The higher the wait function, the slower the LED increases to maximum brightness. I don't know why this happens.

Your current loop does something like this:
Code:
WHILE true
  FOR brightness < 100%
    increase brightness for led1 by &#916;i (=0.05)
    hold brightness and wait for  0.1s until next increase
  END FOR
  // since your while condition is alway true it repeats itself indefinitely, so it should turn the led off and begin to increase the brightness again.
END WHILE
So it is natural that the time it takes to get to 100% brightness is increased with increased wait time within the loop.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Is there a Python Warrior? It's even lazier and spells 'else if' as 'elif'. Plus I wouldn't have to worry about typing out all these 'end' lines or weird !/? method calls.

I haven't tried it myself but google turned this up
 

RELAYER

Banned
Actually the mbed libraries you are using are written in c++ ;)

kdm2FVR.jpg




arit said:
Should work. Did you turn the LED off after bringing it to 100% brighness and before starting the loop for the next LED?
And I suppose that every LED is connected to its own pin? In that case you'd have to create a seperate PwmOut Object for each of it (at least in your code you only did create one for LED1)

It works now. Thanks.
I don't know why I couldn't get it to work earlier. Guess I just needed a night of sleep.
 

RELAYER

Banned
Sorry to post so much but now there's a new problem.

So when the LED is fully on, a piezo attached to the system should make a 1046 frequency sound.

This is my code for that scenario

Code:
PwmOut led(LED1);
PwmOut piezo(p21);

float i;
float frequency = 1046;

int main() {
   while(1) {
      for (i =0; i <= 1.0; i = i + 0.05) {
      led = i; wait(0.1);
         if (led == 1) { //when the led gets to its maximum this loop should be entered
         piezo.period(1/frequency); //this is saying the frequency will be 1046
         piezo = 0.5; //this is saying the duty cycle is 0.5, necessary to hear the piezo
         wait(0.4); //duration of the sound
         }
      }
   }
}

This doesn't work.
I'm guessing for some reason my if-clause isn't being entered. Does anyone know why?

edit: If I use the assign operator instead of the equals operator, it works, but it seems to mess up the LED's modulation. Needs more tinkering I guess...
 

Tamanon

Banned
I am way out of my depth on this one, but shouldn't it be

Code:
 for (i =0; i < 1.0; i = i + 0.05) {

instead of <=? That loop goes up to 1.05.

Again, I have no clue, since I don't know C++ or hardware stuff.
 
^^ Haven't followed everything up this point. But in the general case, I'd recommend against doing floating point comparisons as not every number has an exact fp representation.
 

maeh2k

Member
Sorry to post so much but now there's a new problem.

So when the LED is fully on, a piezo attached to the system should make a 1046 frequency sound.

This is my code for that scenario

Code:
PwmOut led(LED1);
PwmOut piezo(p21);

float i;
float frequency = 1046;

int main() {
   while(1) {
      for (i =0; i <= 1.0; i = i + 0.05) {
      led = i; wait(0.1);
         if (led == 1) { //when the led gets to its maximum this loop should be entered
         piezo.period(1/frequency); //this is saying the frequency will be 1046
         piezo = 0.5; //this is saying the duty cycle is 0.5, necessary to hear the piezo
         wait(0.4); //duration of the sound
         }
      }
   }
}

This doesn't work.
I'm guessing for some reason my if-clause isn't being entered. Does anyone know why?

edit: If I use the assign operator instead of the equals operator, it works, but it seems to mess up the LED's modulation. Needs more tinkering I guess...

I'll give you a couple of general pointers:

If 1046 is a specific constant that you won't change, the code should reflect that.
-> const float FREQUENCY = 1046;

Declare your variables where you actually use them. The float i shouldn't be a global variable. It holds no meaning to the reader at this point.
-> for(float i = ...

Be careful with floats! Comparing floating point numbers doesn't work like it does in mathematics. Never do exact comparisons between two floats. If you want to check if a numbers == 1.0, then you need to check whether it's within a small delta of 1.0.
 

RELAYER

Banned
I've made all the changes you all have recommended.

I made a video that shows what it's doing. Sorry for the weird quality.

https://vimeo.com/106055818

So I think you guys are right in that there is a major problem with the way the two numbers are being compared, because it seems that the if-clauses are being entered multiple times when they should only be entered once at the maximum value for the LED.

If I can't compare the two in this way, how can I change that?
If I declare the frequencies as int, the entire code doesn't work.
How can I check it's within a small delta?
 
I've made all the changes you all have recommended.

I made a video that shows what it's doing. Sorry for the weird quality.

https://vimeo.com/106055818

So I think you guys are right in that there is a major problem with the way the two numbers are being compared, because it seems that the if-clauses are being entered multiple times when they should only be entered once at the maximum value for the LED.

If I can't compare the two in this way, how can I change that?
If I declare the frequencies as int, the entire code doesn't work.
How can I check it's within a small delta?

The usual technique for comparing floating point numbers is something like the following:

Code:
#include <math.h>

float epsilon = 10e-7; // or 0.0000001, I think scientific notation is fine in C
if (abs(floatToCompare - 1.0) < epsilon) { 
    // the value is within 10e-7 of 1.0, so we call it equal
}

This is needed because if you are adding 0.05 on each time, the exact floating point representation might not be exactly 1. For example I just fired up a python interpreter and did the following:

Code:
>>> x = 0.2
>>> x += 0.2
>>> x += 0.2
>>> x
0.6000000000000001

You can read more about it here: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems.

Also as others have said you should consider changing your loop to an integer count if you know how many steps you want to quantise to. Added bonus is you don't even need the floating point comparison I mentioned above. Something like:

Code:
for (i = 0; i <= 20; i++) {
    led = [B]i / 20.0[/B]; 
    wait(0.1);
    if (i == 20) { //when the led gets to its maximum this loop should be entered
        // do stuffs
    }
}

edit: I literally managed to write that last block with ~6 separate bugs the first time.
 

RELAYER

Banned
Tried both epsilon method and int method.
Both result in same behavior depicted in video.
Something must be seriously wrong with the way the code is set up.
Thanks for the help guys but I guess I'll just turn it in like this. I'm really hate when I don't understand something so seemingly basic but I have way too much other stuff due right now.
 

hateradio

The Most Dangerous Yes Man
Does anyone know if printf can display floating points with at most 2 precision digits.

If I use something like '%.2f' I'll get a number like this 1.00, but if it's 1.00 I just want it to show 1. I don't want to show the unnecessary '.00'.

Otherwise I have to use string replacements after doing the printf.
 
Not quite unfortunately. The closest thing to it is using the "%g" specifier, but numeric modifiers to g refer to the entire count of digits not just the decimal place. So you can do:

Code:
printf("%.3g\n", 1.367);
printf("%.3g\n", 1.0);

and get:

Code:
1.37
1

But if you have 22.378 you'll get 22.4. The easiest compromise I can think of without getting messy with string manipulation is to just calculate the number of digits in the first part ahead of time, then add 2 to get your full precision. You can use the ".*g" specifier to use an expression for the precision. Something like:

Code:
#include <stdio.h>

int number_of_digits(int x) {
    int digits = 0;
    do {
        x /= 10;
        digits++;
    } while (x != 0);
    return digits;
}

int main() {
    float foo = 1.367;
    float bar = 1.0;
    float baz = 22.95876;
    printf("%.*g\n", number_of_digits(foo) + 2, foo);
    printf("%.*g\n", number_of_digits(bar) + 2, bar);
    printf("%.*g\n", number_of_digits(baz) + 2, baz);
}

Prints:

Code:
1.37
1
22.96
 

BreakyBoy

o_O @_@ O_o
A non-programming question, but industry related I guess?

I'm being asked to submit a formal report/request for a raise. Unlike a normal raise, I'm more asking for a market correction based on the fact that my job title doesn't reflect what I actually have been doing for about two years. On top of that, there's also the possibility of me moving cross country to be in the same office as the rest of the team. The market there also has a higher salary rate for the industry.

I already know to hit glassdoor as the first stop, and I have some informal information I will include from some unsolicited offers I've received in the last two months, but those of course, will be unsubstantiated. I need actual data to make my case for both the job title salary correction, and then the further correction for relocation if that happens.

TL;DR: I need salary data for developers/IT professionals? Anywhere else but Glassdoor?
 

Mr.Mike

Member
TL;DR: I need salary data for developers/IT professionals? Anywhere else but Glassdoor?

You can try some government sources. (I spent way to much time looking at such things when I was contemplating switching majors).

In America there's the Beureau of Labour Statistics: http://www.bls.gov/ooh/computer-and-information-technology/home.htm

Canada has something similar that's also really good in that it breaks down salaries and data by region (which is something I'm not sure the BLS does), but I'm guessing you're in the US. Perhaps whatever agency does the census in your country would also have such information?
 

danthefan

Member
So I need to learn some Python. I've ended up in a job with a fair amount of programming involved despite having no formal qualifications or work experience.

Is Learn Python The Hard Way a good place to start?
 
Top Bottom