# For loop: InexactError: Bool

**URL:** https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591
**Category:** New to Julia
**Tags:** question
**Created:** [May 5, 2021, 3:39pm UTC](https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591 "2021-05-05T15:39:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Shrox](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@Shrox](https://discourse.julialang.org/u/Shrox)
#### Post date: [May 5, 2021, 3:39pm UTC](https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591/1 "2021-05-05T15:39:31Z")

</div>

Hello everyone!

im really new to Julia and have some problems regarding my code.  
Im trying to do a qr decomposition.

```julia
function qr_zerlegung(A)

    m,n = size(A)
	
	j=1
	
	
	minNM=min(n-1, m);
	
	Q=Matrix(I,m,n)
	
	for j in 1:minNM
		
		
		alpha=1^(sign(A[j,j]))*norm(A[j:n,j],2)
		
		
		
		v=A[j:n,j]+alpha*Matrix(I,n+1-j,1)
		
		
		H=Matrix(I,n,n)
		
		H[j:n,j:n]=Matrix(I,n-j+1,n-j+1)-(2*(v*v')/norm(v,2)^2)
		
		A=H*A
		
		A[j+1:n,j]=0
		
		Q=Q*H
			
	end
	
	R=A

    return Q, R

end

```

I think that the line with

```julia
H[j:n,j:n]=Matrix(I,n-j+1,n-j+1)-(v*v')/norm(v,2)^2

```

produced the InexactError: Bool because of the assignment. Or i think it does 🙈  
Does anyone know how to fix it?

Have a great day!

Shrox

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 5, 2021, 3:52pm UTC](https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591/2 "2021-05-05T15:52:44Z")

</div>

You’re updating `H` in place, but it has an element type of `Bool` — so it can only store `Bool`s:

```julia
julia> H=Matrix(I,3,3)
3×3 Matrix{Bool}:
 1 0 0
 0 1 0
 0 0 1

julia> H[1, 1] = rand()
ERROR: InexactError: Bool(0.7071995471955543)

```

You probably want something like `H=Matrix{eltype(A)}(I,3,3)`.

---

<div class="post-metadata">

### Author: ![Shrox](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@Shrox](https://discourse.julialang.org/u/Shrox)
#### Post date: [May 5, 2021, 4:02pm UTC](https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591/3 "2021-05-05T16:02:37Z")

</div>

hey!  
thanks for the fast reply.

Oh ok. So H=Matrix{eltype(A)}(I,n,n) is producing a Int64

```julia
Matrix(I,n-j+1,n-j+1)-(2*(v*v')/norm(v,2)^2)
3×3 Array{Float64,2}:
  0.57735 -0.57735 0.57735
 -0.57735 0.211325 0.788675
  0.57735 0.788675 0.211325

```

is producing a Float64. So in order to work i need H to be Float64 as well. Is that how it generally works?

I really appreciate the help!

edit:  
with H=Matrix{eltype(A)}(I,n,n)  
i get an InexactError: Int64

Im a bit lost with these datatypes 🙈

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [May 5, 2021, 7:29pm UTC](https://discourse.julialang.org/t/for-loop-inexacterror-bool/60591/4 "2021-05-05T19:29:59Z")

</div>

`H=Matrix{eltype(A)}(I,n,n)` would make a matrix of `eltype(A)`

If you `eltype(A)` is `Int64`, it would have the similar inexact problem; i.e. making a matrix H of `Int64`.  
Meaning, if you make a matrix like this:

```julia
julia> A = [1 2;3 4]
2×2 Array{Int64,2}:
 1 2
 3 4

```

The element would be `Int64`, therefore H would also be element of type `Int64`

```julia
julia> Matrix{eltype(A)}(I,n,n)
3×3 Array{Int64,2}:
 1 0 0
 0 1 0
 0 0 1

```
