Difference between single and double in matlab?

조회 수: 19 (최근 30일)
ABTJ
ABTJ 2020년 2월 23일
편집: David Goodmanson 2020년 2월 23일
Althought both are different data types of MATLAB but what is difference between these two data types. Is it only that double has 64 bits and has more storage place and can store comparativeltly large value??
I was writing a code in MATLAB
clc
close all
clear all
x1=single(550000000)+single(5000000)+single(200000)+single(18300)
x2=550000000+5000000+200000+18300
CX1=class(x1)
CX2=class(x2)
Why do i get different answers for x1 and x2?? The last two lines CX1 and CX2 tell us that x1 is single while x2 is double but why we don't get equal value in both x1 and x2?

채택된 답변

David Goodmanson
David Goodmanson 2020년 2월 23일
편집: David Goodmanson 2020년 2월 23일
Hi ABT
the reason is that you are adding a small number (18300) to a large number (550000000 +5000000 + 200000) and 'single' does not have enough significant figures to accomplish this. Single needs some bits for an exponent and a sign, and there are not enough bits left over (23 in the ieee754 single-precision standard) to do the job. If you replace single with int32, there are enough bits and it works. Here int32 only needs one bit for the sign and the rest of them can contribute to significant figures.
  댓글 수: 2
ABTJ
ABTJ 2020년 2월 23일
how we can replace single with int32?
David Goodmanson
David Goodmanson 2020년 2월 23일
편집: David Goodmanson 2020년 2월 23일
x1 = int32(55e7) etc. See 'doc numeric types', > create numeric variables

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by