3
votes

I've written a prime-generating function generatePrimes (full code here) that takes input bound::Int64 and returns a Vector{Int64} of all primes up to bound. After the function definition, I have the following code:

println("Generating primes...")
println("Last prime: ", generatePrimes(10^7)[end]) 
println("Primes generated.")

which prints, unexpectedly,

Generating primes...
9999991
Primes generated.

This output misses the "Last prime: " segment of the second print statement. The output does work as expected for smaller inputs; any input at least up to 10^6, but somehow fails for 10^7. I've tried several workarounds for this (e.g. assigning the returned value or converting it to a string before calling it in a print statement, combining the print statements, et cetera) and discovered some other weird behaviour: if the "Last prime", is removed from the second print statement, for input 10^7, the last prime doesn't print at all and all I get is a blank line between the first and third print statements. These issues are probably related, and I can't seem to find anything online about why some print statements wouldn't work in Julia.

Thanks so much for any clarification!

Edit: Per DNF's suggestion, following are some reductions to this issue:

  1. Removing the first and last print statements doesn't change anything -- a blank line is always printed in the case I outlined and each of the cases below.
println(generatePrimes(10^7)[end]) # output: empty line
  1. Calling the function and storing the last index in a variable before calling println doesn't change anything either; the cases below work exactly the same either way.
lastPrime::Int = generatePrimes(10^7)[end]
println(lastPrime) # output: empty line
  1. If I call the function in whatever form immediately before a println, an empty line is printed regardless of what's inside the println.
lastPrime::Int = generatePrimes(10^7)[end]
println("This doesn't print") # output: empty line
println("This does print") # output: This does print
  1. If I call the function (or print the pre-generated-and-stored function result) inside a println, anything before the function call (that's also inside the println) isn't printed. The 9999991 and anything else there may be after the function call is printed only if there is something else inside the println before the function call.
# Example 1
println(generatePrimes(10^7)[end]) # output: empty line
# Example 2
println("This first part doesn't print", generatePrimes(10^7)[end]) # output: 9999991
# Example 3
println("This first part doesn't print", generatePrimes(10^7)[end], " prints") # output: 9999991 prints
# Example 4
println(generatePrimes(10^7)[end], "prime doesn't print") # output: prime doesn't print

I could probably list twenty different variations of this same thing, but that probably wouldn't make things any clearer. In every single case version of this issue I've seen so far, the issue only manifests if there's that function call somewhere; println prints large integers just fine. That said, please let me know if anyone feels like they need more info. Thanks so much!

3
This is very odd and shouldn't happen unless you've mucked about with the print function — which is entirely possible with type piracy. Have you perhaps added any methods to the print function? This is hard to debug without a fully working example that can be run in a fresh REPL displaying the behavior you're reporting. - StefanKarpinski
Far from it, it's my first day using Julia -- all I've written is maybe twenty lines of code, and it's a fresh installation of Atom and Juno, so it should be running pure Julia. Judging from Przemyslaw's answer, it seems to be a known issue? - Eshan Uniyal
(If it helps, here's the Julia script) - Eshan Uniyal
Can you reduce the example further? Does this only happen if you run the generatePrimes function first, or does it also happen if you just write println("Last prime: ", 9999991)? What if you remove the other print function calls? It's important to find the the absolute minimum conditions that will reproduce your problem. - DNF
@DNF Good idea, thanks! I've posted some examples and reductions in an edit to the question. - Eshan Uniyal

3 Answers

2
votes

Most likely you are running this code from Atom Juno which recently has some issues with buffering standard output (already reported by others and I also sometimes have this problem). One thing you can try to do is to flush your standard output

flush(stdout)

Like with any unstable bug restarting Atom Juno also seems to help.

0
votes

I had the same issue. For me, changing the terminal renderer (File -> Settings -> Packages -> julia-client -> Terminal Options) from webgl to canvas (see pic below) seems to solve the issue.

change terminal renderer

0
votes

I've also encountered this problem many times. (First time, it was triggered after using the debugger. It is probably unrelated but I have been using Julia+Juno for 2 weeks prior to this issue.)

In my case, the code before the println statement needed to have multiple dictionary assignation (with new keys) in order to trigger the behavior.

I also confirmed that the same code ran in Command Prompt (with same Julia interpreter) prints fine. Any hints about how to further investigate this will be appreciated.

I temporarily solve this issue by printing to stderr, thinking that this stream has more stringent flush mechanism: println(stderr, "hello!")