0

I'm writing a program to compute the determinant of a 2x2 square matrix. It seems to be working fine, but I need to format the determinant result in the system FP(10,4,1). I've tried formatting the matrix inputs in exponential notation but each input is being represented in a different floating point system. For example, if the input matrix is:

A=[[101,101.1],[1.187,1.186]]

I need to convert this matrix to the form:

A1=[[0.0101E+04, 0.1011E+03], [0.1187E+01, 0.1186E+01]].

I've tried formatting using format(num,"e"), but this way it seems that I cannot control how many decimals should be used to represent each number.

m=2
n=2
mat=[[0 for x in range(m)] for y in range(n)]
print("Digite os termos da matriz:")
for i in range(0,2):
    for j in range(0,2):
        num=float(input())
        #num=num.format_float_scientific(num.float32(num.num))
        mat[i][j]=format(num, "e")
print(mat)
#mat[0][0]=mat.format_float_scientific(mat.float32(mat.mat[0][0]))
det=float(mat[0][0])*float(mat[1][1])-float(mat[0][1])*float(mat[1][0])
#det=mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]
print(det)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Does this answer your question? [Display a decimal in scientific notation](https://stackoverflow.com/questions/6913532/display-a-decimal-in-scientific-notation) – andrewJames Apr 14 '20 at 00:29

1 Answers1

0

You are on the right track. According to the docs, you can solve your issue similar to the .2f that is typically used. Look at example 5 on https://python-reference.readthedocs.io/en/latest/docs/functions/format.html

num = 101101.1
newnum = format(num, '.4E')
Sri
  • 2,281
  • 2
  • 17
  • 24
  • Thanks! It's almost right now. The only problem is that I should convert the matrix elements in a way that every number is written in the form +0.dddd*10ˆb or -0.dddd*10ˆb. That is, every number should start with 0, have 4 decimals and the corresponding 10th power. Do you have any ideia how could I do this? – Marcelo Carvalho Apr 14 '20 at 00:48
  • I'm a little confused with your output. Can you give me an example input/output? – Sri Apr 14 '20 at 00:56
  • Sure! For example, for the input matrix elements 101 101.1 1.187 and 1.186, the formatted output should be 0.0101*10ˆ4 0.1011*10ˆ3 0.1187*10ˆ1 and 0.1186*10ˆ1. Is is more clear now? – Marcelo Carvalho Apr 14 '20 at 00:59
  • Oh, I don't think that falls under any of the standard formats. For something like that, you would probably have to convert to a string and manipulate it – Sri Apr 14 '20 at 01:04