Characters and Booleans¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=QtbosKrL2Mk&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=9

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'

Characters¶

C++¶

In [3]:
%%cpp
char c='x'; //attention ' here is important otherwise it is a string in C++ and C
cout << "Ich bin "<<c<<"."<<endl;
Ich bin x.

C¶

In [4]:
%%cpp
char c='x'; //attention ' here is important otherwise it is a string in C++ and C
printf("Ich bin %c.\n",c);
Ich bin x.
In [5]:
%%cpp
printf("Ich bin %c.\n"); //c is dangerous if you forget the paramter you get a stack underflow crash
Error in callback <bound method StreamCapture.post_execute of <JupyROOT.helpers.utils.StreamCapture object at 0x7f67c971b1d0>> (for post_execute), with arguments args (),kwargs {}:
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
File ~/miniforge3/envs/ROOT5/lib/python3.11/site-packages/JupyROOT/helpers/utils.py:400, in StreamCapture.post_execute(self)
    397 self.ioHandler.EndCapture()
    399 # Print for the notebook
--> 400 out = self.ioHandler.GetStdout()
    401 err = self.ioHandler.GetStderr()
    402 if not transformers:

File ~/miniforge3/envs/ROOT5/lib/python3.11/site-packages/JupyROOT/helpers/handlers.py:56, in IOHandler.GetStdout(self)
     55 def GetStdout(self):
---> 56    return _lib.JupyROOTExecutorHandler_GetStdout()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 8: invalid continuation byte

Javascript¶

No character type exist, they are considered strings with length 1.

In [6]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
let c="x"
console.log("Ich bin "+c+".\n");

Python¶

No character type exist, they are considered strings with length 1.

In [7]:
c="x"
print("Ich bin "+c+".\n")
Ich bin x.

Fortran¶

In [8]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    character :: c="X" ! this is a character
    print *,"Hello Mr. ",c
! end program
end subroutine main
In [9]:
main()
 Hello Mr. X

Boolean¶

C++¶

In [10]:
%%cpp
bool old=true,young=false;
cout <<"I am "<<old<<" and "<<young<<"."<<endl;
I am 1 and 0.

C¶

C does not have boolean type. We just use int.

In [11]:
%%cpp
int old=1,young=0;
cout <<"I am "<<old<<" and "<<young<<"."<<endl;
I am 1 and 0.

Javascript¶

In [12]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let old=true,young=false;
console.log("I am "+old+" and "+young+".\n")

Python¶

In [13]:
old=True;young=False;
print("I am",old,"and",young,".\n")
I am True and False .

Fortran¶

In [14]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    logical :: old=.true. 
    logical :: young=.false.
    print *,"I am",old,"and",young
! end program
end subroutine main
In [15]:
main()
 I am T and F