@rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S "%0" %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #### #### All Code by Steve Michel #### #!/usr/bin/perl use Math::Trig; #line 16 # creates multiple OBJ files ###################################### ## ## ## CONSTANTS ## ## use Math::Trig; $radians = deg2rad($degrees); $degrees = rad2deg($radians) ###################################### use constant PI => 4 * atan2(1, 1); ###################################### ## ## ## Define Variables ## ## $range ### This is the size of the object - if "4" then -2 is low and 2 is high ## $stepZ ### How many "Ribs" - smaller number produces more Ribs for more resolution ## $stepX ### Dist between points on each Rib - smaller number produces more Ribs for more resolution # ### Note: if step equal step2 then faces will be roughly square ## $amp ### Height of Wave ## $FallOff ### ## $freq ### Number of waves in range ## $funct ### Note currently only Sin supported - future to add Cos ## $StartDeg ### Recommend Zero ## $EndDeg ### Recommend 360 (testing with smaller number OK) ## $SkipDeg ## $RibTotal = (## $range/## $step1)+1; ## $XYTotal = (## $range/## $step2)+1; ## $VertexTotal = ## $RibTotal * ## $XYTotal; ## $zLow = ## $range/-2; ## $zHigh = ## $range/2; ## $xLow = ## $range/-2; ## $xHigh = ## $range/2; ## $xRange = ## $range/2; ## ## $fill = "", "0" or "00" for padding to three places ## ## $RibMake is Loop Counter for making each Rib ## $RibStepZ is Z depth for each Rib ## $RibStepX is current X Position on each Rib ## ## ## ## ## ## ## ###################################### ###################################### ## ## primary equations ## ## Hypotenuse = ((a**2) + (b**2)) ** .5 ## ## Y = Amp * sin(Freq * X + phi) + offset ## ## ###################################### ###################################### ## ## ## USER SETTINGS ## ## ###################################### $range = 8; ### This is the size of the object - if "4" then -2 is low and 2 is high $stepZ = .05; ### How many "Ribs" - smaller number produces more Ribs for more resolution $stepX = .05; ### Dist between points on each Rib - smaller number produces more Ribs for more resolution ### Note: if stepZ equal stepX then faces will be roughly square $amp = .3; ### Height of Wave $freq = 3; ### Number of waves $funct = "Sin"; ### Note currently only Sin and Cos supported (if not "Cos" then it is Sin $StartDeg = 0; ### Recommend Zero $EndDeg = 360; ### Recommend 360 (testing with smaller number OK) $SkipDeg = 2; ### Recommend 1,2,3,4,5,6,9 or 10 (result is 360,180,120,90,72,60,40 or 36 OBJ files) $ColorRed = 44; ### 0-255 $ColorGreen = 199; ### 0-255 $ColorBlue = 183; ### 0-255 $Specularity = 20; ### 0-100 ###################################### ## ## ## END USER SETTINGS ## ## ###################################### if ($SkipDeg <= 0){$SkipDeg = 4;} if ($funct ne "Cos"){ $funct = "Sin";} ###################################### ## ## ## Deduced Settings ## ## ###################################### $step1 = $stepZ; $step2 = $stepX; $RibTotal = ($range/$step1)+1; $XYTotal = ($range/$step2)+1; $VertexTotal = $RibTotal * $XYTotal; $FaceTotal = ($range/$step1) * ($range/$step2); $UseFaceTotal = ($range/$step2) * $RibTotal ; $zLow = $range/-2; $zHigh = $range/2; $xLow = $range/-2; $xHigh = $range/2; $xRange = $range/2; ## print "Number of Ribs = " . $RibTotal; ## print "\n"; ## print "Number of Faces = " . $FaceTotal; ## print "\n"; ## print "Number per Rib = " . $XYTotal; ## print "\n"; ## print "Total Vertices = " . $VertexTotal; ## print "\n"; ## print "Z depth = " . $zLow; ## print "\n"; ## print "Z forward " . $zHigh; ## print "\n"; ## print "X to left = " . $xLow; ## print "\n"; ## print "X to right = " . $xHigh; ## print "\n"; ###################################### ## ## ## END Deduced Settings ## ## ###################################### ###################################### ## ## Start $Create Object Loop ## ###################################### $Create = 0; $ObjectCount =1; #for ($Create = $StartDeg; $Create <= $EndDeg ; $Create = $Create + $SkipDeg){ # if ($Create <=999){$fill = "" ;} # if ($Create <=99){$fill = "0" ;} # if ($Create <=9){$fill = "00" ;} # print "create OBJ # " . $Create . "\n"; #create OBJ SURF file # open (FILEobj, ">C:\\Projects\\3D-design\\SIN\\Perl\\dxf\\TrigWaveOBJ_SURF_" . $range . "_" . $freq . "_" . $funct . $fill . $Create . ".obj"); # close FILEobj; #create OBJ SURF FILEobj headers # open (FILEobj, ">>C:\\Projects\\3D-design\\SIN\\Perl\\dxf\\TrigWaveOBJ_SURF_" . $range . "_" . $freq . "_" . $funct . $fill . $Create . ".obj"); # print FILEobj ("####" . "\n"); # print FILEobj ("#" . "\n"); # print FILEobj ("# OBJ FILE Generated by LightWave 3D(TM)" . "\n"); # print FILEobj ("#" . "\n"); # print FILEobj ("####" . "\n"); # print FILEobj ("# Object: TrigWaveOBJ_SURF" . "\n"); # print FILEobj ("#" . "\n"); # print FILEobj ("# Vertices: " . $VertexTotal . "\n"); # print FILEobj ("# Points: 0" . "\n"); # print FILEobj ("# Lines: 0" . "\n"); # print FILEobj ("# Faces: 0" . "\n"); # print FILEobj ("# Materials: 1" . "\n"); # print FILEobj ("#" . "\n"); # print FILEobj ("####" . "\n"); # print FILEobj ("\n"); # print FILEobj ("# o TrigWaveOBJ_SURF" . "\n"); # print FILEobj ("\n"); # print FILEobj ("# Vertex list" . "\n"); # print FILEobj ("\n"); # close FILEobj; # } ###################################### ## ## END $Create Object Loop ## ###################################### ###################################### ## ## Start $Calc vertex Loop ## ###################################### $Calc = $StartDeg; for ($Calc = $StartDeg; $Calc <= $EndDeg ; $Calc = $Calc + $SkipDeg){ if ($Calc <=999){$fill = "" ;} if ($Calc <=99){$fill = "0" ;} if ($Calc <=9){$fill = "00" ;} $bBoxXlow[$Calc] = 999999; $bBoxXHigh[$Calc] = -999999; $bBoxYlow[$Calc] = 999999; $bBoxYHigh[$Calc] = -999999; $bBoxZlow[$Calc] = 999999; $bBoxZHigh[$Calc] = -999999; $TESTYHypo = (($xLow)**2 + ($zLow)**2)**.5; $TESTYHypo2 = (int($TESTYHypo * 100000))/100000; # open (FILEobj, ">>C:\\Projects\\3D-design\\SIN\\Perl\\dxf\\TrigWaveOBJ_SURF_" . $range . "_" . $freq . "_" . $funct . $fill . $Calc . ".obj"); ## Start $RibMake vertex Loop $RibStepZ = $zLow; $TotalPoint = 1; $StoreHexXYZ[$Calc] = ""; for ($RibMake = 1; $RibMake <= $RibTotal; $RibMake++){ print $Calc . "calculating Rib# " . $RibMake . " of " . $RibTotal . "\n"; ## Start $CalcY vertex Loop $RibStepX = $xLow; for ($CalcY = 1; $CalcY <= $XYTotal; $CalcY++){ $firstHypo = (($RibStepZ)**2 + ($RibStepX)**2)**.5; $firstHypo2 = (int($firstHypo * 100000))/100000; ##################### ### ### ### Convert $Calc from degree to Rad ### ### use $CalcRad for phase!!!! ### ### ##################### $CalcRad = deg2rad($Calc); if ($funct eq "Sin"){ $yHeight = sin(($firstHypo2 * $freq)+ $CalcRad); $yHeight = (int($yHeight *100000))/100000;} if ($funct eq "Cos"){ $yHeight = cos(($firstHypo2 * $freq)+ $CalcRad); $yHeight = (int($yHeight *100000))/100000;} $yHeight = $yHeight * $amp; $yHeight = $yHeight * ( 1 - ( $firstHypo2 * (1 / $TESTYHypo2))); $yHeight = (int($yHeight *100000))/100000; # print FILEobj ("v " . $RibStepX . " " . $yHeight . " " . $RibStepZ); # print FILEobj ("\n"); $XDeciVal = $RibStepX ; $YDeciVal = $yHeight ; $ZDeciVal = $RibStepZ ; $Test = $XDeciVal; &FloatPoint; if ($XDeciVal eq "0"){ $HexBits = "00000000"; } $hexX = $HexBits; $Test = $YDeciVal; &FloatPoint; if ($YDeciVal eq "0"){ $HexBits = "00000000"; } $hexY = $HexBits; $Test = $ZDeciVal; &FloatPoint; if ($ZDeciVal eq "0"){ $HexBits = "00000000"; } $hexZ = $HexBits; if ($XDeciVal < $bBoxXlow[$Calc]){ $bBoxXlow[$Calc] = $XDeciVal; } if ($YDeciVal < $bBoxYlow[$Calc]){ $bBoxYlow[$Calc] = $YDeciVal; } if ($ZDeciVal < $bBoxZlow[$Calc]){ $bBoxZlow[$Calc] = $ZDeciVal; } if ($XDeciVal > $bBoxXHigh[$Calc]){ $bBoxXHigh[$Calc] = $XDeciVal; } if ($YDeciVal > $bBoxYHigh[$Calc]){ $bBoxYHigh[$Calc] = $YDeciVal; } if ($ZDeciVal > $bBoxZHigh[$Calc]){ $bBoxZHigh[$Calc] = $ZDeciVal; } $hexXYZ = $hexX . $hexY . $hexZ; $StoreHexXYZ[$Calc] = $StoreHexXYZ[$Calc] . $hexXYZ; $TempRibStepX = ($RibStepX + $step2); $TempRibStepXtemp = int($TempRibStepX * 1000005); $RibStepX = $TempRibStepXtemp / 1000000; $RibStepX = (int($RibStepX *1000))/1000; $TotalPoint++; }## End $CalcY vertex Loop $TempRibStepZ = ($RibStepZ + $step1); $TempRibStepZtemp = int($TempRibStepZ * 1000005); $RibStepZ = $TempRibStepZtemp / 1000000; $RibStepZ = (int($RibStepZ *1000))/1000; }## End $RibMake vertex Loop # close FILEobj; } print "\n"; ###################################### ## ## END $Calc vertex Loop ## ###################################### ###################################### ## ## Start $Calc Face Loop ## ###################################### $Calc = 0; $PolyCount = 1; $ObjectCount =1; for ($Calc = $StartDeg; $Calc <= $EndDeg ; $Calc = $Calc + $SkipDeg){ if ($Calc <=999){$fill = "" ;} if ($Calc <=99){$fill = "0" ;} if ($Calc <=9){$fill = "00" ;} # open (FILEobj, ">>C:\\Projects\\3D-design\\SIN\\Perl\\dxf\\TrigWaveOBJ_SURF_" . $range . "_" . $freq . "_" . $funct . $fill . $Calc . ".obj"); # print FILEobj ("\n"); # print FILEobj ("# Face list" . "\n"); # print FILEobj ("\n"); $FaceCounter = 1; $FaceHexBuild = ""; for ($FaceMake = 1; $FaceMake < $UseFaceTotal; $FaceMake++){ if ($FaceCounter == $XYTotal){ $FaceCounter = 1; $FaceMake++; } print $Calc . "calculating Face# " . $FaceMake . " of " . $UseFaceTotal . "\n"; $Vertex1 = $FaceMake; $Vertex2 = $FaceMake + 1; $Vertex3 = $FaceMake + $XYTotal + 1; $Vertex4 = $FaceMake + $XYTotal; # print FILEobj ("f "); # print FILEobj ($Vertex1 . " "); # print FILEobj ($Vertex4 . " "); # print FILEobj ($Vertex3 . " "); # print FILEobj ($Vertex2 . " "); # print FILEobj ("\n"); $LWO_Vertex1 = $Vertex1 - 1; $LWO_Vertex2 = $Vertex2 - 1; $LWO_Vertex3 = $Vertex3 - 1 ; $LWO_Vertex4 = $Vertex4 - 1; $hexLWOVertex1 = sprintf("%04x", $LWO_Vertex1); $hexLWOVertex2 = sprintf("%04x", $LWO_Vertex2); $hexLWOVertex3 = sprintf("%04x", $LWO_Vertex3); $hexLWOVertex4 = sprintf("%04x", $LWO_Vertex4); $FACE[$PolyCount] = "0004" . $hexLWOVertex2 . $hexLWOVertex3 . $hexLWOVertex4 . $hexLWOVertex1; $FaceHexBuild[$Calc] = $FaceHexBuild[$Calc] . $FACE[$PolyCount]; # # print FILEobj ("f "); # # print FILEobj ($Vertex1 . " "); # # print FILEobj ($Vertex2 . " "); # # print FILEobj ($Vertex3 . " "); # # print FILEobj ($Vertex4 . " "); # # print FILEobj ("\n"); $FaceCounter++; $PolyCount++; }## End $RibMake Face Loop # print FILEobj ("\n"); # print FILEobj ("# End of file" . "\n"); # close FILEobj; $TotalPointByte = int($VertexTotal * 12); $hexTotalPointByte = sprintf("%08x", $TotalPointByte); # print $hexTotalPointByte; # print "\n"; $Test = $bBoxXlow[$Calc]; &FloatPoint; $HexbBoxXlow = $HexBits; $Test = $bBoxYlow[$Calc]; &FloatPoint; $HexbBoxYlow = $HexBits; $Test = $bBoxZlow[$Calc]; &FloatPoint; $HexbBoxZlow = $HexBits; $Test = $bBoxXHigh[$Calc]; &FloatPoint; $HexbBoxXHigh = $HexBits; $Test = $bBoxYHigh[$Calc]; &FloatPoint; $HexbBoxYHigh = $HexBits; $Test = $bBoxZHigh[$Calc]; &FloatPoint; $HexbBoxZHigh = $HexBits; $HexbBoxExtents[$Calc] = $HexbBoxXlow . $HexbBoxYlow . $HexbBoxZlow . $HexbBoxXHigh . $HexbBoxYHigh . $HexbBoxZHigh; $ByteTotalFace = int(($FaceTotal * 10)+ 4); $HexTotalPoly = sprintf("%04x", $ByteTotalFace); $StringLWO[1] = "464F524D"; # $StringLWO[1] = "FORM"; in hex $StringLWO[2] = "000073AE"; ## will get edit to be the total number of bytes -8 bytes # $StringLWO[3] = "4C574F324C41595200000012000000000000000000000000000000000000504E5453"; # $StringLWO[3] = "LWO2LAYR......................PNTS";in hex $StringLWO[3] = "4C574F32544147530000000844656661756C74004C41595200000012000000000000000000000000000000000000504E5453"; # $StringLWO[3] = "LWO2TAGS....Default.LAYR......................PNTS";in hex ## 4C574F32544147530000000844656661756C74004C41595200000012000000000000000000000000000000000000504E5453 $StringLWO[4] = $hexTotalPointByte; $StringLWO[5] = ""; # $StringLWO[5] = all vertex coordinates $StringLWO[5] = $StoreHexXYZ[$Calc]; $StringLWO[6] = "42424F5800000018"; # $StringLWO[6] = "BBOX...."; in hex $StringLWO[7] = $HexbBoxExtents[$Calc]; # $StringLWO[8] = "POLS"; $StringLWO[8] = "504F4C53"; # $StringLWO[9] = $hexTotalFaceByte; $ByteTotalFace = int(($FaceTotal * 10)+ 4); $HexTotalPoly = sprintf("%08x", $ByteTotalFace); $StringLWO[9] = $HexTotalPoly; # $StringLWO[10] = "FACE"; $StringLWO[10] = "46414345"; # $StringLWO[11] = all Face things; $StringLWO[11] = $FaceHexBuild[$Calc]; # $StringLWO[12] = "PTAG"; plus length of StringLWO[13] + SURF; $StringLWO[12] = "50544147"; # $StringLWO[13] = all faces 0-> total ( if there are 80, then 0 thru 79 ) $StringLWO[13] = ""; for ($ThisFaceSurfaceLoop = 1; $ThisFaceSurfaceLoop < $FaceTotal; $ThisFaceSurfaceLoop++){ $HexFaceNumberBuild = sprintf("%08x", $ThisFaceSurfaceLoop); $StringLWO[13] = $StringLWO[13] . $HexFaceNumberBuild; } $LenStringLWO[13] = ( (length ($StringLWO[13]) ) /2 ) + 8; $hexLenStringLWO[13] = sprintf("%08x", $LenStringLWO[13]); $StringLWO[12] = $StringLWO[12] . $hexLenStringLWO[13] . "535552460000"; # $StringLWO[14] = "0000535552460000003E44656661756C74000000434F4C52000E"; $StringLWO[14] = "0000535552460000004844656661756C74000000434F4C52000E"; # # $StringLWO[14] = "..SURF...>Default...COLR.."; # $ColorRed = 65; # $ColorGreen = 90; # $ColorBlue = 190; $ColorRedVal = $ColorRed / 255; $Test = $ColorRedVal; &FloatPoint; $HexColorRedVal = $HexBits; $ColorGreenVal = $ColorGreen / 255; $Test = $ColorGreenVal; &FloatPoint; $HexColorGreenVal = $HexBits; $ColorBlueVal = $ColorBlue / 255; $Test = $ColorBlueVal; &FloatPoint; $HexColorBlueVal = $HexBits; #$Specularity = 40; $SpecVal = $Specularity / 100; $Test = $SpecVal; &FloatPoint; if ($SpecVal eq "0"){$HexBits = "00000000";} if ($SpecVal = 0){$HexBits = "00000000";} $HexSpecVal = $HexBits; print $HexSpecVal; print "\n"; $StringLWO[15] = $HexColorRedVal; # $StringLWO[15] = Color Red Channel 0.000 -> .999999 (0-255) in hex; $StringLWO[16] = $HexColorGreenVal; # $StringLWO[16] = Color Green Channel 0.000 -> .999999 (0-255) in hex; $StringLWO[17] = $HexColorBlueVal; # $StringLWO[17] = Color Blue Channel 0.000 -> .999999 (0-255) in hex; # $StringLWO[18] = "00004449464600063F80000000005350454300060000000000005349444500020003"; # $StringLWO[18] = "00004449464600063F80000000005350454300063E23D70A00005349444500020003"; $StringLWO[18] = "00004449464600063F8000000000535045430006"; # $StringLWO[18] = "..DIFF..?€....SPEC.. $StringLWO[19] = $HexSpecVal; $StringLWO[20] = "0000534D414E0004"; # $StringLWO[20] = "..SMAN.."; $StringLWO[21] = "3FAE40F1"; ## smoothing angle in radians in float hex $StringLWO[22] = "5349444500020003"; print $StringLWO[15] . " " . $StringLWO[16] . " " . $StringLWO[17]; print "\n"; $LongString = ""; for ($PrintLoop = 1;$PrintLoop<=22;$PrintLoop++){ $LongString = $LongString . $StringLWO[$PrintLoop]; # print $hexPrintLoop[$PrintLoop]; } $LongString = $LongString . $HexbBoxExtents; $LenLongString = length ($LongString); $LenLongStringUSE = (int($LenLongString) /2 ) - 8 ; # print $LenLongStringUSE; $HexLengthLS = sprintf("%08x", $LenLongStringUSE); # print " "; # print $HexLengthLS; # print "\n"; $StringLWO[2] = $HexLengthLS; $LongString = ""; for ($PrintLoop = 1;$PrintLoop<=22;$PrintLoop++){ $LongString = $LongString . $StringLWO[$PrintLoop]; } if ($Calc <=999){$fill = "" ;} if ($Calc <=99){$fill = "0" ;} if ($Calc <=9){$fill = "00" ;} open (OUTPUT_LWO, ">C:\\Projects\\3D-design\\SIN\\Perl\\dxf\\TrigWaveLWO_SURF_" . $funct . $range . "_" . $freq . "_" . $fill . $Calc . ".lwo"); binmode (OUTPUT_LWO); $fmt_type = 0; my $dstring = pack('H*',$LongString); print OUTPUT_LWO $dstring; close OUTPUT_LWO; ###End Calc LWO loop } ########################################################################## ########################################################################## ## ### ## ### ## sub routine to convert dec values to floating point ### ## ### ## ########################################################################## ########################################################################## sub FloatPoint { # print "object #" . $Calc . " Vertex #"; # print $NewPointLoop . " "; # print "source " . $Test . "\n"; if ($Test >= 0){ $Sign = 0;} if ($Test < 0){ $Sign = 1;} $IntTest = int($Test); $AbsIntTest = abs($IntTest); # print "Abs of " . $IntTest . " ABS = " . $AbsIntTest . "\n"; $BinTest = sprintf ("%b",$AbsIntTest); # print "Integer " . $IntTest . " Binary of Integer " . $BinTest . "\n"; $right = int(abs ($Test - int($Test))*100000000); $right = $right / 100000000; # print "Stuff to Right of decimal " . $right . "\n"; $NewBin = $BinTest; $LenNewBin = length($NewBin); $LessLenNewBin = $LenNewBin -1; $MantissaBinStart = substr($NewBin,1,$LessLenNewBin); # print "length of Binary is " . $LenNewBin ; # print "\n"; ########### if integer is 1 or greater then determine exponent if ($Test == 0){ $HexBits = "00000000"; } if ($Test eq "0" ){ $HexBits = "00000000"; } if ($AbsIntTest >= 1){ for($expTest = 0; $expTest <=128;$expTest ++){ # print "potential Binary " . $NewBin . " potential exp " . $expTest; # print "\n"; if (int($NewBin)==1){ $ExpUSE = $expTest + 127; $BinExpUSE = sprintf ("%b",$ExpUSE); $Mantissa = $NewBin; $expTest = 128; } $NewBin = $NewBin /10; } $PrintExp = $ExpUSE; # print "exp + 127 for positive integers = " . $ExpUSE . " and in Binary = " . $BinExpUSE; # print "\n"; # print "Sign = " . $Sign; # print "\n"; ################# BreakDown $Right $LoopThing[1] = $right; # print $LoopThing[1]; # print "\n"; # print "\n"; $BinLoop = 1; $LoopThing[$BinLoop] = $LoopThing[$BinLoop] * 10000000; for ($BinLoop = 1; $BinLoop<=(26);$BinLoop++){ $NextThing[$BinLoop] = $LoopThing[$BinLoop] * 2; $NextThing[$BinLoop] =($NextThing[$BinLoop])/10000000; $BinVal[$BinLoop] = int($NextThing[$BinLoop]); # print $BinLoop . " " . ($LoopThing[$BinLoop]/10000000) . " x 2 = " . $NextThing[$BinLoop] . " with Bin Val = " . $BinVal[$BinLoop]; # print "\n"; $DealWithFP_Int = $NextThing[$BinLoop] * 10000000; $DealWithFP_Dec = int($NextThing[$BinLoop]) * 10000000; $DealWithFP = $DealWithFP_Int - $DealWithFP_Dec; # print $DealWithFP_Int . " " . $DealWithFP_Dec . " " . $DealWithFP; # print "\n"; $LoopThing[$BinLoop + 1] = (($DealWithFP)); }#End BinLoop $ExponentLength = length($BinExpUSE); $ExpFill = ""; if ($ExponentLength == 8){$ExpFill = "";} if ($ExponentLength == 7){$ExpFill = "0";} if ($ExponentLength == 6){$ExpFill = "00";} if ($ExponentLength == 5){$ExpFill = "000";} if ($ExponentLength == 4){$ExpFill = "0000";} if ($ExponentLength == 3){$ExpFill = "00000";} if ($ExponentLength == 2){$ExpFill = "000000";} if ($ExponentLength == 1){$ExpFill = "0000000";} if ($ExponentLength == 0){$ExpFill = "00000000";} # print "sign " . $Sign; # print "\n"; # print "exponent " . $ExpFill . $BinExpUSE; # print "\n"; # print "mantissa 1." .$MantissaBinStart; # print " "; $BinAdd = "" ; for ($BinLoop = 1; $BinLoop<=(25);$BinLoop++){ $BinAdd = $BinAdd . $BinVal[$BinLoop]; # print $BinVal[$BinLoop] . ""; } # print "\n"; # print "hello world 2" . "\n"; $MantissaPrint = $MantissaBinStart . $BinAdd; $TotalBinPrep = $Sign . $ExpFill . $BinExpUSE . $MantissaBinStart . $BinAdd; # print "\n"; # print $TotalBinPrep; # print "\n"; $LenTotalBin= length($TotalBinPrep); # print $LenTotalBin; # print "\n"; $TotalBin = substr($TotalBinPrep,0,32); # print $TotalBin; ##########Now break it into bits - 4 bit pieces $Increment=0; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ $BitPrep[$BitLoop] = substr($TotalBin,$Increment,4); # print $BitPrep[$BitLoop]; #print " "; $Increment = $Increment +4; } # print "\n"; # print "\n"; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ # print $BitPrep[$BitLoop]; # print " "; } # print "\n"; $adHexBit = ""; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ if ($BitPrep[$BitLoop] eq "0000"){$HexPrep[$BitLoop] = "0"}; if ($BitPrep[$BitLoop] eq "0001"){$HexPrep[$BitLoop] = "1"}; if ($BitPrep[$BitLoop] eq "0010"){$HexPrep[$BitLoop] = "2"}; if ($BitPrep[$BitLoop] eq "0011"){$HexPrep[$BitLoop] = "3"}; if ($BitPrep[$BitLoop] eq "0100"){$HexPrep[$BitLoop] = "4"}; if ($BitPrep[$BitLoop] eq "0101"){$HexPrep[$BitLoop] = "5"}; if ($BitPrep[$BitLoop] eq "0110"){$HexPrep[$BitLoop] = "6"}; if ($BitPrep[$BitLoop] eq "0111"){$HexPrep[$BitLoop] = "7"}; if ($BitPrep[$BitLoop] eq "1000"){$HexPrep[$BitLoop] = "8"}; if ($BitPrep[$BitLoop] eq "1001"){$HexPrep[$BitLoop] = "9"}; if ($BitPrep[$BitLoop] eq "1010"){$HexPrep[$BitLoop] = "A"}; if ($BitPrep[$BitLoop] eq "1011"){$HexPrep[$BitLoop] = "B"}; if ($BitPrep[$BitLoop] eq "1100"){$HexPrep[$BitLoop] = "C"}; if ($BitPrep[$BitLoop] eq "1101"){$HexPrep[$BitLoop] = "D"}; if ($BitPrep[$BitLoop] eq "1110"){$HexPrep[$BitLoop] = "E"}; if ($BitPrep[$BitLoop] eq "1111"){$HexPrep[$BitLoop] = "F"}; $adHexBit = $adHexBit . $HexPrep[$BitLoop]; } $HexBits = $adHexBit; } #End if ($AbsIntTest >= 1) ############################################### ############################################### # # # now working out the ABS val less than 1 # # ############################################### ############################################### else { if ($AbsIntTest < 1){ # print "ABS VAlUE OF INTEGER IS LESS THAN ONE - solution soon"; # print "\n"; # print $Test; # print "\n"; # print $right $Recursion = 50; $LoopThing[1] = $right; $BinLoop = 1; $LoopThing[$BinLoop] = $LoopThing[$BinLoop] * 10000000; for ($BinLoop = 1; $BinLoop<=$Recursion;$BinLoop++){ $NextThing[$BinLoop] = $LoopThing[$BinLoop] * 2; $NextThing[$BinLoop] =($NextThing[$BinLoop])/10000000; $BinVal[$BinLoop] = int($NextThing[$BinLoop]); # print $BinLoop . " " . ($LoopThing[$BinLoop]/10000000) . " x 2 = " . $NextThing[$BinLoop] . " with Bin Val = " . $BinVal[$BinLoop]; # print "\n"; $DealWithFP_Int = $NextThing[$BinLoop] * 10000000; $DealWithFP_Dec = int($NextThing[$BinLoop]) * 10000000; $DealWithFP = $DealWithFP_Int - $DealWithFP_Dec; # print $DealWithFP_Int . " " . $DealWithFP_Dec . " " . $DealWithFP; # print "\n"; $LoopThing[$BinLoop + 1] = (($DealWithFP)); }#End BinLoop $AddBinVal = ""; for ($BinLoop = 1; $BinLoop<=$Recursion;$BinLoop++){ # print $BinVal[$BinLoop]; $AddBinVal = $AddBinVal . $BinVal[$BinLoop]; } # print "\n"; # print "hello world"; # print $AddBinVal; # print "\n"; for ($BinLoop = 1; $BinLoop<=$Recursion;$BinLoop++){ if ($BinVal[$BinLoop] ==1){ $ExpTemp = $BinLoop; $ExpNow = $BinLoop * -1; $ExpNowUSE = $ExpNow + 127; $BinLoop = $Recursion; } } # $BitPrep[$BitLoop] = substr($TotalBin,$Increment,4); $MantissaPrep = substr($AddBinVal,$ExpTemp,23); for($gapfill = 1;$gapfill <= $ExpTemp; $gapfill++){ # print "x"; } # print "\n"; # print "hello world"; # print $MantissaPrep; # print "\n"; # print "\n"; $PrintExp = $ExpNowUSE; $BinExpUSE = sprintf ("%b",$ExpNowUSE); $ExponentLength = length($BinExpUSE); $ExpFill = ""; if ($ExponentLength == 8){$ExpFill = "";} if ($ExponentLength == 7){$ExpFill = "0";} if ($ExponentLength == 6){$ExpFill = "00";} if ($ExponentLength == 5){$ExpFill = "000";} if ($ExponentLength == 4){$ExpFill = "0000";} if ($ExponentLength == 3){$ExpFill = "00000";} if ($ExponentLength == 2){$ExpFill = "000000";} if ($ExponentLength == 1){$ExpFill = "0000000";} if ($ExponentLength == 0){$ExpFill = "00000000";} # print "\n"; # print $ExpNow . " Exponent " . $ExpNowUSE . " and in bin = " . $ExpFill . $BinExpUSE; # print "\n"; # print "\n"; $MantissaPrint = $MantissaPrep; # print "sign " . $Sign; # print "\n"; # print "exponent " . $ExpFill . $BinExpUSE; # print "\n"; # print "mantissa 1." . $MantissaPrint; # print "\n"; # print "\n"; $TotalBin = $Sign . $ExpFill . $BinExpUSE . $MantissaPrep; # print "Binary " . $TotalBin; ##########Now break it into bits - 4 bit pieces $Increment=0; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ $BitPrep[$BitLoop] = substr($TotalBin,$Increment,4); # print $BitPrep[$BitLoop]; # print " "; $Increment = $Increment +4; } # print "\n"; # print "\n"; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ # print $BitPrep[$BitLoop]; # print " "; } # print "\n"; $adHexBit = ""; for($BitLoop = 1; $BitLoop <= 8; $BitLoop++){ if ($BitPrep[$BitLoop] eq "0000"){$HexPrep[$BitLoop] = "0"}; if ($BitPrep[$BitLoop] eq "0001"){$HexPrep[$BitLoop] = "1"}; if ($BitPrep[$BitLoop] eq "0010"){$HexPrep[$BitLoop] = "2"}; if ($BitPrep[$BitLoop] eq "0011"){$HexPrep[$BitLoop] = "3"}; if ($BitPrep[$BitLoop] eq "0100"){$HexPrep[$BitLoop] = "4"}; if ($BitPrep[$BitLoop] eq "0101"){$HexPrep[$BitLoop] = "5"}; if ($BitPrep[$BitLoop] eq "0110"){$HexPrep[$BitLoop] = "6"}; if ($BitPrep[$BitLoop] eq "0111"){$HexPrep[$BitLoop] = "7"}; if ($BitPrep[$BitLoop] eq "1000"){$HexPrep[$BitLoop] = "8"}; if ($BitPrep[$BitLoop] eq "1001"){$HexPrep[$BitLoop] = "9"}; if ($BitPrep[$BitLoop] eq "1010"){$HexPrep[$BitLoop] = "A"}; if ($BitPrep[$BitLoop] eq "1011"){$HexPrep[$BitLoop] = "B"}; if ($BitPrep[$BitLoop] eq "1100"){$HexPrep[$BitLoop] = "C"}; if ($BitPrep[$BitLoop] eq "1101"){$HexPrep[$BitLoop] = "D"}; if ($BitPrep[$BitLoop] eq "1110"){$HexPrep[$BitLoop] = "E"}; if ($BitPrep[$BitLoop] eq "1111"){$HexPrep[$BitLoop] = "F"}; $adHexBit = $adHexBit . $HexPrep[$BitLoop]; } $HexBits = $adHexBit; } ## End if ($Test > 0) } ## end Else $PrintExpUse = $ExpFill . $BinExpUSE; # $decExp = sprintf("%d", $int); # print "\n"; # print "Exp xxxxx " . $PrintExp; $FinalExp = $PrintExp - 127; if ($Sign eq "0"){$SignText = "Pos";} if ($Sign eq "1"){$SignText = "Neg";} if ($Sign eq "0"){$SignValue = 1;} if ($Sign eq "1"){$SignValue = -1;} # print "\n"; # print "sign " . $Sign . " = " . $SignText; # print "\n"; # print "exponent " . $PrintExpUse . " decimal = " . $PrintExp . " - 127 = " . $FinalExp; # print "\n"; $MantissaPrint23 = substr($MantissaPrint,0,23); $LenMantissaPrint = length($MantissaPrint23); # print $LenMantissaPrint . " "; # $MantissaPrint = "1." . $MantissaPrint23; $MantissaPrint = "(1.)" . $MantissaPrint23; # print "mantissa " . $MantissaPrint; # print "\n"; $DecTotal = 1; $DecInc = 0; for($NewDecLoop=0;$NewDecLoop<=23;$NewDecLoop++){ $DecBuild[$NewDecLoop] = substr($MantissaPrint23,$DecInc,1); # print $DecBuild[$NewDecLoop]; $DecInc = $DecInc + 1; } # print "\n"; for($NewDecLoop=0;$NewDecLoop<=24;$NewDecLoop++){ # print $DecBuild[$NewDecLoop]; } # print "\n"; # print "\n"; for($NewDecLoop=0;$NewDecLoop<=24;$NewDecLoop++){ $DecExpBuild[$NewDecLoop] = ($DecBuild[$NewDecLoop]) * (2**(($NewDecLoop+1) * -1)); $DecTotal = $DecTotal + $DecExpBuild[$NewDecLoop]; # print $DecExpBuild[$NewDecLoop]; # print "\n"; } $DecTotal = $SignValue * ($DecTotal * (2**$FinalExp)); # $NewDec = ($MantissaPrint * (2**$FinalExp)); # print "\n"; # print "\n"; # print "decimal value:"; # print "\n"; # print $DecTotal; # print "\n"; # print "\n"; # print "Single precision (32 bits):"; # print "\n"; # print $TotalBin; # print "\n"; # print $HexBits; # end } # end FloatPoint