Typecasting¶
you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=5iL-onZ6M5U&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=13
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'
C++¶
In [3]:
%%cpp
cout <<int(7.8)<<endl;
cout <<7/8<<endl; //attention here
cout <<double(7/8)<<endl; //that also does not do the job
cout <<7./8<<endl; //thats ok
cout <<7/8.<<endl; //thats ok
7 0 0 0.875 0.875
C¶
In [4]:
%%cpp
printf("%d",(int)7.8);
7
Javascript¶
javascript does not distinguish between integers and floating point numbers.
https://www.w3schools.com/js/js_type_conversion.asp
In [5]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}
console.log(7/8)
console.log(Number("1")+1)
console.log(String(1)+1)
console.log(Boolean(1))
In [ ]:
Python¶
In [6]:
print(7/8)
print(int(7.8))
print(int("7"))
print(float("7.2")+1.1)
print(str(2)+str(1.1))
print(bool(1))
print(bool(0))
0.875 7 7 8.3 21.1 True False
Fortran¶
In [7]:
%%fortran
! program and subroutine exchanged due to jupyternotebook
! program main
subroutine main()
implicit none
integer :: stat
integer :: i
real*8 :: d
character*20 :: s
character*30 :: s2
print *,7/8
print *,int(7.8d0)
i=7;d=7.2
write(s,"(i5)") i
print *,s
write(s,"(f10.5)") d
print *,s
write(s,"(es20.5)") d
print *,s
i=2;d=2.1
read(s,*,iostat=stat) i
read(s2,*,iostat=stat) d
print *,i,d
!print *,logical(1)
!print *,logical(0)
! end program
end subroutine main
In [8]:
main()
0
7
7
7.20000
7.20000E+00
2 2.0999999046325684